Skip to content

Commit 8912949

Browse files
committed
Add support for Scala Native's --c-compile-option and --cpp-compile-option
1 parent 63fb5ae commit 8912949

File tree

10 files changed

+180
-0
lines changed

10 files changed

+180
-0
lines changed

modules/build/src/test/scala/scala/build/tests/ScalaNativeUsingDirectiveTests.scala

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,48 @@ class ScalaNativeUsingDirectiveTests extends TestUtil.ScalaCliBuildSuite {
150150
}
151151
}
152152

153+
for { directiveKey <- Seq("nativeCCompile", "native-c-compile") }
154+
test(s"ScalaNativeOptions for $directiveKey") {
155+
val expectedOption1 = "compileOption1"
156+
val expectedOption2 = "compileOption2"
157+
val inputs = TestInputs(
158+
os.rel / "p.sc" ->
159+
s"""//> using $directiveKey $expectedOption1 $expectedOption2
160+
|def foo() = println("hello foo")
161+
|""".stripMargin
162+
)
163+
164+
inputs.withLoadedBuild(buildOptions, buildThreads, bloopConfig) { (_, _, maybeBuild) =>
165+
assert(
166+
maybeBuild.options.scalaNativeOptions.cCompileOptions.head == expectedOption1
167+
)
168+
assert(
169+
maybeBuild.options.scalaNativeOptions.cCompileOptions.drop(1).head == expectedOption2
170+
)
171+
}
172+
}
173+
174+
for { directiveKey <- Seq("nativeCppCompile", "native-cpp-compile") }
175+
test(s"ScalaNativeOptions for $directiveKey") {
176+
val expectedOption1 = "compileOption1"
177+
val expectedOption2 = "compileOption2"
178+
val inputs = TestInputs(
179+
os.rel / "p.sc" ->
180+
s"""//> using $directiveKey $expectedOption1 $expectedOption2
181+
|def foo() = println("hello foo")
182+
|""".stripMargin
183+
)
184+
185+
inputs.withLoadedBuild(buildOptions, buildThreads, bloopConfig) { (_, _, maybeBuild) =>
186+
assert(
187+
maybeBuild.options.scalaNativeOptions.cppCompileOptions.head == expectedOption1
188+
)
189+
assert(
190+
maybeBuild.options.scalaNativeOptions.cppCompileOptions.drop(1).head == expectedOption2
191+
)
192+
}
193+
}
194+
153195
test("ScalaNativeOptions for native-linking and no value") {
154196
val inputs = TestInputs(
155197
os.rel / "p.sc" ->

modules/cli/src/main/scala/scala/cli/commands/shared/ScalaNativeOptions.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ final case class ScalaNativeOptions(
5555
@Tag(tags.should)
5656
nativeCompile: List[String] = Nil,
5757

58+
@Group(HelpGroup.ScalaNative.toString)
59+
@HelpMessage("List of compile options (C files only)")
60+
@Tag(tags.should)
61+
nativeCCompile: List[String] = Nil,
62+
63+
@Group(HelpGroup.ScalaNative.toString)
64+
@HelpMessage("List of compile options (C++ files only)")
65+
@Tag(tags.should)
66+
nativeCppCompile: List[String] = Nil,
67+
5868
@Group(HelpGroup.ScalaNative.toString)
5969
@Hidden
6070
@HelpMessage("Use default compile options")

modules/cli/src/main/scala/scala/cli/commands/shared/SharedOptions.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ final case class SharedOptions(
273273
linkingOptions = nativeLinking,
274274
linkingDefaults = nativeLinkingDefaults,
275275
compileOptions = nativeCompile,
276+
cCompileOptions = nativeCCompile,
277+
cppCompileOptions = nativeCppCompile,
276278
compileDefaults = nativeCompileDefaults,
277279
embedResources = embedResources,
278280
buildTargetStr = nativeTarget,

modules/directives/src/main/scala/scala/build/preprocessing/directives/ScalaNative.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import scala.cli.commands.SpecificationLevel
1212
@DirectiveExamples(s"//> using nativeLto full")
1313
@DirectiveExamples(s"//> using nativeVersion ${Constants.scalaNativeVersion}")
1414
@DirectiveExamples(s"//> using nativeCompile -flto=thin")
15+
@DirectiveExamples(s"//> using nativeCCompile -std=c17")
16+
@DirectiveExamples(s"//> using nativeCppCompile -std=c++17 -fcxx-exceptions")
1517
@DirectiveExamples(s"//> using nativeLinking -flto=thin")
1618
@DirectiveExamples(s"//> using nativeClang ./clang")
1719
@DirectiveExamples(s"//> using nativeClangPP ./clang++")
@@ -32,6 +34,10 @@ import scala.cli.commands.SpecificationLevel
3234
|
3335
|`//> using nativeCompile` _value1_ _value2_ …
3436
|
37+
|`//> using nativeCCompile` _value1_ _value2_ …
38+
|
39+
|`//> using nativeCppCompile` _value1_ _value2_ …
40+
|
3541
|`//> using nativeLinking` _value1_ _value2_ …
3642
|
3743
|`//> using nativeClang` _value_
@@ -59,6 +65,8 @@ final case class ScalaNative(
5965
nativeLto: Option[String] = None,
6066
nativeVersion: Option[String] = None,
6167
nativeCompile: List[String] = Nil,
68+
nativeCCompile: List[String] = Nil,
69+
nativeCppCompile: List[String] = Nil,
6270
nativeLinking: List[String] = Nil,
6371
nativeClang: Option[String] = None,
6472
@DirectiveName("nativeClangPp")
@@ -74,6 +82,8 @@ final case class ScalaNative(
7482
ltoStr = nativeLto,
7583
version = nativeVersion,
7684
compileOptions = nativeCompile,
85+
cCompileOptions = nativeCCompile,
86+
cppCompileOptions = nativeCppCompile,
7787
linkingOptions = nativeLinking,
7888
clang = nativeClang,
7989
clangpp = nativeClangPP,

modules/options/src/main/scala/scala/build/options/ScalaNativeOptions.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ final case class ScalaNativeOptions(
3636
linkingOptions: List[String] = Nil,
3737
linkingDefaults: Option[Boolean] = None,
3838
compileOptions: List[String] = Nil,
39+
cCompileOptions: List[String] = Nil,
40+
cppCompileOptions: List[String] = Nil,
3941
compileDefaults: Option[Boolean] = None,
4042
embedResources: Option[Boolean] = None,
4143
buildTargetStr: Option[String] = None,
@@ -109,6 +111,10 @@ final case class ScalaNativeOptions(
109111

110112
private def compileCliOptions(): List[String] =
111113
finalCompileOptions().flatMap(option => List("--compile-option", option))
114+
private def cCompileCliOptions(): List[String] =
115+
cCompileOptions.flatMap(option => List("--c-compile-option", option))
116+
private def cppCompileCliOptions(): List[String] =
117+
cppCompileOptions.flatMap(option => List("--cpp-compile-option", option))
112118
private def ltoOptions(): List[String] =
113119
ltoStr.map(_.trim).filter(_.nonEmpty)
114120
.map(lto => LTO.apply(lto))
@@ -176,6 +182,8 @@ final case class ScalaNativeOptions(
176182
clangppCliOption() ++
177183
linkingCliOptions() ++
178184
compileCliOptions() ++
185+
cCompileCliOptions() ++
186+
cppCompileCliOptions() ++
179187
resourcesCliOptions(resourcesExist) ++
180188
targetCliOption() ++
181189
multithreadingCliOption()

website/docs/reference/cli-options.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,14 @@ Use default linking settings
14551455

14561456
List of compile options
14571457

1458+
### `--native-c-compile`
1459+
1460+
List of compile options (C files only)
1461+
1462+
### `--native-cpp-compile`
1463+
1464+
List of compile options (C++ files only)
1465+
14581466
### `--native-compile-defaults`
14591467

14601468
[Internal]

website/docs/reference/directives.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,10 @@ Add Scala Native options
505505

506506
`//> using nativeCompile` _value1_ _value2_
507507

508+
`//> using nativeCCompile` _value1_ _value2_
509+
510+
`//> using nativeCppCompile` _value1_ _value2_
511+
508512
`//> using nativeLinking` _value1_ _value2_
509513

510514
`//> using nativeClang` _value_
@@ -534,6 +538,10 @@ Add Scala Native options
534538

535539
`//> using nativeCompile -flto=thin`
536540

541+
`//> using nativeCCompile -std=c17`
542+
543+
`//> using nativeCppCompile -std=c++17 -fcxx-exceptions`
544+
537545
`//> using nativeLinking -flto=thin`
538546

539547
`//> using nativeClang ./clang`

website/docs/reference/scala-command/cli-options.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,18 @@ Use default linking settings
904904

905905
List of compile options
906906

907+
### `--native-c-compile`
908+
909+
`SHOULD have` per Scala Runner specification
910+
911+
List of compile options (C files only)
912+
913+
### `--native-cpp-compile`
914+
915+
`SHOULD have` per Scala Runner specification
916+
917+
List of compile options (C++ files only)
918+
907919
### `--native-compile-defaults`
908920

909921
`IMPLEMENTATION specific` per Scala Runner specification

website/docs/reference/scala-command/directives.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,10 @@ Add Scala Native options
321321

322322
`//> using nativeCompile` _value1_ _value2_
323323

324+
`//> using nativeCCompile` _value1_ _value2_
325+
326+
`//> using nativeCppCompile` _value1_ _value2_
327+
324328
`//> using nativeLinking` _value1_ _value2_
325329

326330
`//> using nativeClang` _value_
@@ -350,6 +354,10 @@ Add Scala Native options
350354

351355
`//> using nativeCompile -flto=thin`
352356

357+
`//> using nativeCCompile -std=c17`
358+
359+
`//> using nativeCppCompile -std=c++17 -fcxx-exceptions`
360+
353361
`//> using nativeLinking -flto=thin`
354362

355363
`//> using nativeClang ./clang`

website/docs/reference/scala-command/runner-specification.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@ Extra options passed to `clang` verbatim during linking
200200

201201
List of compile options
202202

203+
**--native-c-compile**
204+
205+
List of compile options (C files only)
206+
207+
**--native-cpp-compile**
208+
209+
List of compile options (C++ files only)
210+
203211
**--native-target**
204212

205213
Build target type
@@ -994,6 +1002,14 @@ Extra options passed to `clang` verbatim during linking
9941002

9951003
List of compile options
9961004

1005+
**--native-c-compile**
1006+
1007+
List of compile options (C files only)
1008+
1009+
**--native-cpp-compile**
1010+
1011+
List of compile options (C++ files only)
1012+
9971013
**--native-target**
9981014

9991015
Build target type
@@ -1589,6 +1605,14 @@ Extra options passed to `clang` verbatim during linking
15891605

15901606
List of compile options
15911607

1608+
**--native-c-compile**
1609+
1610+
List of compile options (C files only)
1611+
1612+
**--native-cpp-compile**
1613+
1614+
List of compile options (C++ files only)
1615+
15921616
**--native-target**
15931617

15941618
Build target type
@@ -2210,6 +2234,14 @@ Extra options passed to `clang` verbatim during linking
22102234

22112235
List of compile options
22122236

2237+
**--native-c-compile**
2238+
2239+
List of compile options (C files only)
2240+
2241+
**--native-cpp-compile**
2242+
2243+
List of compile options (C++ files only)
2244+
22132245
**--native-target**
22142246

22152247
Build target type
@@ -2850,6 +2882,14 @@ Extra options passed to `clang` verbatim during linking
28502882

28512883
List of compile options
28522884

2885+
**--native-c-compile**
2886+
2887+
List of compile options (C files only)
2888+
2889+
**--native-cpp-compile**
2890+
2891+
List of compile options (C++ files only)
2892+
28532893
**--native-target**
28542894

28552895
Build target type
@@ -3466,6 +3506,14 @@ Extra options passed to `clang` verbatim during linking
34663506

34673507
List of compile options
34683508

3509+
**--native-c-compile**
3510+
3511+
List of compile options (C files only)
3512+
3513+
**--native-cpp-compile**
3514+
3515+
List of compile options (C++ files only)
3516+
34693517
**--native-target**
34703518

34713519
Build target type
@@ -4119,6 +4167,14 @@ Extra options passed to `clang` verbatim during linking
41194167

41204168
List of compile options
41214169

4170+
**--native-c-compile**
4171+
4172+
List of compile options (C files only)
4173+
4174+
**--native-cpp-compile**
4175+
4176+
List of compile options (C++ files only)
4177+
41224178
**--native-target**
41234179

41244180
Build target type
@@ -4832,6 +4888,14 @@ Extra options passed to `clang` verbatim during linking
48324888

48334889
List of compile options
48344890

4891+
**--native-c-compile**
4892+
4893+
List of compile options (C files only)
4894+
4895+
**--native-cpp-compile**
4896+
4897+
List of compile options (C++ files only)
4898+
48354899
**--native-target**
48364900

48374901
Build target type
@@ -5801,6 +5865,14 @@ Extra options passed to `clang` verbatim during linking
58015865

58025866
List of compile options
58035867

5868+
**--native-c-compile**
5869+
5870+
List of compile options (C files only)
5871+
5872+
**--native-cpp-compile**
5873+
5874+
List of compile options (C++ files only)
5875+
58045876
**--native-target**
58055877

58065878
Build target type

0 commit comments

Comments
 (0)