Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Scala Native Examples #3657

Merged
merged 54 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
3346097
Create Foo.scala
c0d33ngr Oct 3, 2024
52a38c4
Add files via upload
c0d33ngr Oct 3, 2024
367bcb0
import scala native lib properly
c0d33ngr Oct 3, 2024
a48faac
Update Foo.scala
c0d33ngr Oct 3, 2024
7df4367
Create FooTests.scala
c0d33ngr Oct 3, 2024
cee766e
Update build.mill
c0d33ngr Oct 3, 2024
ef802d3
Create Hello world.c
c0d33ngr Oct 3, 2024
11a6e75
Create HelloWorld.scala
c0d33ngr Oct 3, 2024
2783005
Rename Hello world.c to HelloWorld.c
c0d33ngr Oct 3, 2024
24b9543
update interop example
c0d33ngr Oct 3, 2024
b1015e7
Create build.mill
c0d33ngr Oct 3, 2024
8740b01
Create Makefile
c0d33ngr Oct 3, 2024
19ed282
Update build.mill of interop example
c0d33ngr Oct 3, 2024
2c9d4ec
add some example files
c0d33ngr Oct 4, 2024
61f53b7
add changes to 2-interop example
c0d33ngr Oct 5, 2024
9dc9053
update scala-native examples code
c0d33ngr Oct 5, 2024
4f3a61b
update scala=native example code
c0d33ngr Oct 5, 2024
dfb0e5e
Create draft build.mill for multi-module example
c0d33ngr Oct 5, 2024
bd1f756
Create MyResource.txt
c0d33ngr Oct 5, 2024
e41d609
Create MyOtherResource.txt
c0d33ngr Oct 5, 2024
69ffd7d
add common-config code
c0d33ngr Oct 6, 2024
7cada73
add doc file for scala-native examples
c0d33ngr Oct 6, 2024
bf2edb2
add draft build.mill for common-config example
c0d33ngr Oct 6, 2024
8dc0853
update 1-simple example code
c0d33ngr Oct 6, 2024
a367ead
fix the errors in 1-simple and 2-interop examples
c0d33ngr Oct 7, 2024
55c143f
add third party dependency to 1-simple
c0d33ngr Oct 8, 2024
c37e9a0
update code to fix some errors
c0d33ngr Oct 9, 2024
24729ea
updated code examples
c0d33ngr Oct 12, 2024
f047193
fix syntax in 1-simple example build.mill file
c0d33ngr Oct 12, 2024
3919933
update code
c0d33ngr Oct 12, 2024
950440a
use task in build.mill
c0d33ngr Oct 17, 2024
dda37bf
add pathref in build.mill
c0d33ngr Oct 17, 2024
ca18f5f
update 4-common-config to scala native specific configs
c0d33ngr Oct 18, 2024
7cbf92e
update the build.mill files
c0d33ngr Oct 20, 2024
82ed7c0
rework the scala native examples
c0d33ngr Oct 21, 2024
ba260e5
add 1-simple to testsuite
c0d33ngr Oct 23, 2024
aed36a5
add 4-common-config to testsuite
c0d33ngr Oct 23, 2024
d98e8c2
add 2-interop to testsuit
c0d33ngr Oct 23, 2024
45fb020
update 1-simple/build.mill to pass CI tests
c0d33ngr Oct 23, 2024
1c5e946
update code examples
c0d33ngr Oct 27, 2024
8659821
fix CI error for 1-simple test
c0d33ngr Oct 27, 2024
6b5b1bf
fix CI error for 3-multi-module test
c0d33ngr Oct 27, 2024
956f223
fix CI test error
c0d33ngr Oct 27, 2024
f2c0e9e
complete multi-module example
c0d33ngr Oct 28, 2024
5768cbd
header file for foo in multi-module
c0d33ngr Oct 28, 2024
7e312b2
remove cause of failing CI tests
c0d33ngr Oct 28, 2024
8d3e300
update build.mill for native examples
c0d33ngr Oct 29, 2024
495c44f
update build.mill for 2-interop and 3-multi-module example
c0d33ngr Oct 29, 2024
4501c2e
Update 1-simple build.mill
c0d33ngr Oct 30, 2024
9242768
update code
c0d33ngr Oct 30, 2024
76bb361
update 1-simple build file
c0d33ngr Oct 31, 2024
a599da6
fix merge conflict
c0d33ngr Oct 31, 2024
9ac2cc1
Merge branch 'main' into add-scalanative-doc
c0d33ngr Oct 31, 2024
084dfb4
add some changes
c0d33ngr Oct 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update build.mill for native examples
  • Loading branch information
c0d33ngr committed Oct 29, 2024
commit 8d3e30044e4852a986276e7b2e7a05e3720a33b2
41 changes: 7 additions & 34 deletions example/scalalib/native/1-simple/build.mill
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//// SNIPPET:BUILD
package build
import mill._, scalalib._, scalanativelib._

Expand All @@ -16,41 +15,15 @@ object `package` extends RootModule with ScalaNativeModule {
def testFramework = "utest.runner.Framework"
}
}
//// SNIPPET:END
//
//
//// SNIPPET:DEPENDENCIES
//
//
/** Usage


> ./mill nativeLink # Build and link native binary
...
compiling 1 Scala source to ...

> ./mill releaseMode # Generate some files in `out` directory about the `releaseMode`. Default releaseMode is set to Debug.

> ./mill nativeOptimize # Generate some files in `out` directory about native optimization. The Default value is true.

> ./mill nativeIncrementalCompilation # Generate config files in `out` directory about native incremental compilation. Default is false.
// This example demonstrates a simple Scala program that generates HTML content
// from user-provided text and prints it to the standard output, utilizing Scala
// Native for native integration and `mainargs` for command-line argument parsing.

> ./mill nativeLinkingOptions # Generate config files in `out` directory about the path in native linking options. Default path is `"-L/usr/local/lib`.
/** Usage

> ./mill show nativeLink # Build and link native binary
...
done compiling

*/

//// SNIPPET:END

//
// You can run `+mill resolve __+` to see a full list of the different tasks that
// are available, `+mill resolve _+` to see the tasks within `foo`,
// `mill inspect compile` to inspect a task's doc-comment documentation or what
// it depends on, or `mill show foo.scalaVersion` to show the output of any task.
//
// The most common *tasks* that Mill can run are cached *targets*, such as
// `compile`, and un-cached *commands* such as `foo.run`. Targets do not
// re-evaluate unless one of their inputs changes, whereas commands re-run every
// time.


22 changes: 13 additions & 9 deletions example/scalalib/native/2-interop/build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@ object `package` extends RootModule with ScalaNativeModule {
def ivyDeps = Agg(ivy"com.lihaoyi::utest::0.8.4")
def testFramework = "utest.runner.Framework"
}

def nativeLinkingOptions = Seq("-L" + millSourcePath.toString + "/target")

// Compiled C
def nativeCompiled = T {
os.makeDir.all(millSourcePath / "target")
// Additional source folder to put C sources
def nativeSources = Task.Sources(millSourcePath / "resources/scala-native")

def nativeLinkingOptions = Seq("-L" + Task.dest.toString)

// Compiled C
def nativeCompiled = Task {
val cSourceFiles = nativeSources().map(_.path).flatMap(os.walk(_)).filter(_.ext == "c")
val output = "libHelloWorld.so"

os.proc("gcc", "-m64", "-shared",
"-c", millSourcePath.toString + "/resources/scala-native/HelloWorld.c",
"-o", millSourcePath.toString + "/target/libHelloWorld.so"
"-c", cSourceFiles,
"-o", output
).call(stdout = os.Inherit)

PathRef(T.dest / "target/libHelloWorld.so")
PathRef(Task.dest / output)
}
}

Expand Down Expand Up @@ -53,6 +57,7 @@ object `package` extends RootModule with ScalaNativeModule {
/** Usage

> ./mill nativeCompiled
...

> ./mill run
Running HelloWorld function
Expand All @@ -63,4 +68,3 @@ Reversed: !dlroW ,olleH
Tests: 1, Passed: 1, Failed: 0

*/

37 changes: 16 additions & 21 deletions example/scalalib/native/3-multi-module/build.mill
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//// SNIPPET:BUILD
package build
import mill._, scalalib._, scalanativelib._

Expand All @@ -11,20 +10,24 @@ trait MyModule extends ScalaNativeModule {
def testFramework = "utest.runner.Framework"
}

def nativeLinkingOptions = Seq("-L" + millSourcePath.toString + "/target")
def barSource = millSourcePath.toString + "/resources/scala-native"
// Additional source folder to put C sources
def nativeSources = Task.Sources(millSourcePath / "resources/scala-native")

def nativeLinkingOptions = Seq("-L" + Task.dest.toString)
//def barSource = millSourcePath.toString + "/resources/scala-native"

// Compile C
def nativeCompiled = Task {
os.makeDir.all(millSourcePath / "target")
val cSourceFiles = nativeSources().map(_.path).flatMap(os.walk(_)).filter(_.ext == "c")
val output = "libHelloWorldBar.so"

os.proc(
"gcc", "-m64", "-shared", "-fPIC",
millSourcePath.toString + "/resources/scala-native/HelloWorldBar.c",
"-o", millSourcePath.toString + "/target/libHelloWorldBar.so")
cSourceFiles,
"-o", output)
.call(stdout = os.Inherit)

PathRef(Task.dest / "target/libHelloWorldBar.so")
PathRef(Task.dest / output)
}

}
Expand All @@ -35,24 +38,22 @@ object foo extends MyModule {
def ivyDeps = Agg(ivy"com.lihaoyi::mainargs::0.7.6")

// Compile C
def nativeCompiled = T {
os.makeDir.all(millSourcePath / "target")
def nativeCompiled = Task {
val cSourceFiles = nativeSources().map(_.path).flatMap(os.walk(_)).filter(_.ext == "c")
val output = "libHelloWorldFoo.so"

os.proc(
"gcc", "-m64", "-shared", "-fPIC",
"-I", barSource,
millSourcePath.toString + "/resources/scala-native/HelloWorldFoo.c",
"-o", millSourcePath.toString + "/target/libHelloWorldFoo.so")
cSourceFiles,
"-o", output)
.call(stdout = os.Inherit)

PathRef(T.dest / "target/libHelloWorldFoo.so")
PathRef(T.dest / output)
}
}

object bar extends MyModule {
def bar = "Bar"
}
//// SNIPPET:END

// This example contains a simple Mill build with two modules, `foo` and `bar`.
// We don't mark either module as top-level using `extends RootModule`, so
Expand All @@ -67,8 +68,6 @@ object bar extends MyModule {
//
// The above builds expect the following project layout:
//
//// SNIPPET:TREE
//
// ----
// build.mill
// bar/
Expand All @@ -90,14 +89,10 @@ object bar extends MyModule {
// Foo.scala
//
// ----
//
//// SNIPPET:END

//

/** Usage


> ./mill bar.run hello
Running HelloWorld function
Done...
Expand Down
21 changes: 8 additions & 13 deletions example/scalalib/native/4-common-config/build.mill
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
// This example shows some of the common tasks you may want to override on a
// `ScalaNativeModule`: specifying the `releaseMode`, `nativeLinkStubs`
// `nativeIncrementalCompilation, `nativeLinkingOptions` and `nativeWorkdir`.


//// SNIPPET:BUILD
package build
import mill._, scalalib._, scalanativelib._, scalanativelib.api._

Expand All @@ -17,23 +11,24 @@ object `package` extends RootModule with ScalaNativeModule {
)

// Set the releaseMode to ReleaseFast.
def releaseMode: T[ReleaseMode] = T { ReleaseMode.ReleaseFast }
def releaseMode: T[ReleaseMode] = ReleaseMode.ReleaseFast

// Set linking of `@stub` methods to true.
def nativeLinkStubs: T[Boolean] = T { true }
def nativeLinkStubs: T[Boolean] = true

// Set incremental compilation to true
def nativeIncrementalCompilation: T[Boolean] = T { true }
def nativeIncrementalCompilation: T[Boolean] = true

// Set nativeLinkingOptions path to a directory named `target`.
def nativeLinkingOptions = Seq("-L" + millSourcePath.toString + "/target")

// Set nativeWorkdir directory to `newDir`
def nativeWorkdir = T { T.dest / "newDir" }
def nativeWorkdir = T.dest / "newDir"
}
//// SNIPPET:END

//
// This example shows some of the common tasks you may want to override on a
// `ScalaNativeModule`: specifying the `releaseMode`, `nativeLinkStubs`
// `nativeIncrementalCompilation, `nativeLinkingOptions` and `nativeWorkdir`.

/** Usage

Expand All @@ -55,5 +50,5 @@ true

> ./mill show nativeWorkdir
...
*/

*/
Loading