Skip to content

Commit fc815fe

Browse files
authored
Merge pull request #161 from jonas/doc-update
Reorganize and improve documentation
2 parents b4e14e3 + 11c4af7 commit fc815fe

File tree

33 files changed

+492
-397
lines changed

33 files changed

+492
-397
lines changed

.scalafmt.conf

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ style = defaultWithAlign
22
docstrings = JavaDoc
33
assumeStandardLibraryStripMargin = true
44
project.excludeFilters = [
5-
tests/samples
5+
"tests/samples/[^/]*[.]scala"
6+
"docs/src/test/scala/org/example/.*[.]scala"
7+
"sbt-scala-native-bindgen/.*/expected/.*[.]scala"
68
]
79
project.git = true
810
runner.dialect = Scala211

.travis.yml

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
language: cpp
1+
language: scala
22
sudo: required
3-
compiler: gcc
3+
os: linux
4+
dist: trusty
5+
jdk: oraclejdk8
46

5-
script:
7+
scala:
8+
- "2.11.12"
9+
10+
before_script:
611
- docker-compose build $TEST_ENV
12+
13+
script:
714
- docker-compose run $TEST_ENV
8-
- if [[ "$TEST_ENV" = *llvm-5.0 ]]; then
9-
docker-compose run --rm $TEST_ENV scripts/scalafmt --test;
10-
docker-compose run --rm $TEST_ENV scripts/clangfmt --test;
11-
fi
1215
- if [[ "$TEST_ENV" = *llvm-6.0 ]]; then
1316
export VERSION="${TRAVIS_COMMIT}";
1417
docker-compose build bindgen;
@@ -29,6 +32,12 @@ matrix:
2932
on:
3033
branch: master
3134
- env: TEST_ENV=ubuntu-18.04-llvm-5.0
35+
- env: TEST_ENV=formatting
36+
before_script:
37+
- clang-format --version
38+
script:
39+
- scripts/scalafmt --test;
40+
- scripts/clangfmt --test;
3241

3342
before_cache:
3443
# See https://www.scala-sbt.org/1.0/docs/Travis-CI-with-sbt.html
@@ -39,4 +48,5 @@ before_cache:
3948
cache:
4049
directories:
4150
- "$HOME/.ivy2/cache"
42-
- "$HOME/.sbt/boot"
51+
- "$HOME/.sbt/boot/scala-*"
52+
- "$HOME/.coursier"

CONTRIBUTING.md

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ cd tests
3030
sbt ~test
3131
```
3232

33+
To run the same tests as is run by Travis CI use the `test.sh` script, which will both build the executable and run `sbt verify`:
34+
35+
```sh
36+
./scripts/test.sh
37+
```
38+
3339
## Coding Guidelines
3440

3541
Code should be formatted with `./scripts/scalafmt` and `./scripts/clangfmt`.

build.sbt

+72-42
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import scala.sys.process._
2+
import org.scalanative.bindgen.sbt.ScalaNativeBindgenPlugin
23

3-
addCommandAlias("verify", "; ^test:compile ; ^test ; ^scripted ; docs/makeSite")
4+
addCommandAlias("verify",
5+
"; tests/test ; docs/test ; ^scripted ; docs/makeSite")
46

57
val Versions = new {
68
val scala210 = "2.10.6"
@@ -88,6 +90,7 @@ lazy val tests = project("tests")
8890
),
8991
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test
9092
)
93+
.aggregate(samples)
9194

9295
lazy val samples = project("samples")
9396
.in(file("tests/samples"))
@@ -98,47 +101,7 @@ lazy val samples = project("samples")
98101
libraryDependencies += "com.lihaoyi" %%% "utest" % "0.6.3" % "test",
99102
testFrameworks += new TestFramework("utest.runner.Framework"),
100103
nativeLinkStubs := true,
101-
Test / nativeLinkingOptions += {
102-
Seq("-L", (Test / target).value.getAbsoluteFile / "bindgen").mkString
103-
},
104-
Test / compile := {
105-
val log = streams.value.log
106-
val cwd = (Test / target).value / "bindgen"
107-
val compileOptions = (Test / nativeCompileOptions).value
108-
val cpaths = (baseDirectory.value.getAbsoluteFile * "*.c").get
109-
val clangPath = nativeClang.value.toPath.toAbsolutePath.toString
110-
111-
cwd.mkdirs()
112-
113-
def abs(path: File): String =
114-
path.getAbsolutePath
115-
116-
def run(command: Seq[String]): Int = {
117-
log.info("Running " + command.mkString(" "))
118-
Process(command, cwd) ! log
119-
}
120-
121-
val opaths = cpaths.map { cpath =>
122-
val opath = abs(cwd / s"${cpath.getName}.o")
123-
val command = Seq(clangPath) ++ compileOptions ++ Seq("-c",
124-
abs(cpath),
125-
"-o",
126-
opath)
127-
128-
if (run(command) != 0) {
129-
sys.error(s"Failed to compile $cpath")
130-
}
131-
opath
132-
}
133-
134-
val archivePath = cwd / "libbindgentests.a"
135-
val archive = Seq("ar", "cr", abs(archivePath)) ++ opaths
136-
if (run(archive) != 0) {
137-
sys.error(s"Failed to create archive $archivePath")
138-
}
139-
140-
(Test / compile).value
141-
}
104+
compileTask("bindgentests", baseDirectory)
142105
)
143106

144107
lazy val tools = project("tools")
@@ -166,16 +129,33 @@ lazy val sbtPlugin = project("sbt-scala-native-bindgen", ScriptedPlugin)
166129

167130
lazy val docs = project("docs")
168131
.enablePlugins(GhpagesPlugin, ParadoxSitePlugin, ParadoxMaterialThemePlugin)
132+
.enablePlugins(ScalaNativePlugin, ScalaNativeBindgenPlugin)
169133
.settings(
170134
publish / skip := true,
135+
scalaVersion := Versions.scala211,
136+
// FIXME: Remove when a version has been released with Test scope settings.
137+
ScalaNativeBindgenPlugin.nativeBindgenScopedSettings(Test),
138+
Test / nativeBindings += {
139+
NativeBinding((Test / resourceDirectory).value / "vector.h")
140+
.name("vector")
141+
.link("vector")
142+
.packageName("org.example")
143+
},
144+
Test / nativeBindgen / target := (Test / scalaSource).value / "org/example",
145+
nativeLinkStubs := true,
146+
compileTask("vector", Test / resourceDirectory),
147+
libraryDependencies += "org.scalatest" %%% "scalatest" % "3.2.0-SNAP10" % Test,
171148
Paradox / paradoxProperties ++= Map(
172149
"github.base_url" -> scmInfo.value.get.browseUrl.toString
173150
),
174151
ParadoxMaterialThemePlugin.paradoxMaterialThemeSettings(Paradox),
175152
Paradox / paradoxMaterialTheme := {
153+
val licenseUrl = scmInfo.value.get.browseUrl.toString + "blob/master/LICENSE.txt"
176154
(Paradox / paradoxMaterialTheme).value
177155
.withRepository(scmInfo.value.get.browseUrl.toURI)
178156
.withColor("indigo", "indigo")
157+
.withCopyright(
158+
s"Copyright © Liudmila Kornilova, distributed under the <a href='$licenseUrl'>Scala license</a>.")
179159
}
180160
)
181161

@@ -209,3 +189,53 @@ def project(name: String, plugged: AutoPlugin*) = {
209189

210190
lazy val setReleaseVersions: String => State => State =
211191
v => _.put(ReleaseKeys.versions, (v, v))
192+
193+
def compileTask(libname: String, srcDirTask: SettingKey[File]) = Def.settings(
194+
Test / nativeLinkingOptions += {
195+
Seq("-L", (Test / target).value.getAbsoluteFile / "bindgen").mkString
196+
},
197+
Test / compile := {
198+
val log = streams.value.log
199+
val cwd = (Test / target).value / "bindgen"
200+
val compileOptions = (Test / nativeCompileOptions).value
201+
val cpaths = (srcDirTask.value.getAbsoluteFile * "*.c").get
202+
val clangPath = nativeClang.value.toPath.toAbsolutePath.toString
203+
204+
cwd.mkdirs()
205+
206+
def abs(path: File): String =
207+
path.getAbsolutePath
208+
209+
def run(command: Seq[String]): Int = {
210+
log.info("Running " + command.mkString(" "))
211+
Process(command, cwd) ! log
212+
}
213+
214+
val opaths = cpaths.map {
215+
cpath =>
216+
val opath = cwd / s"${cpath.getName}.o"
217+
val command = Seq(clangPath) ++ compileOptions ++ Seq("-c",
218+
abs(cpath),
219+
"-o",
220+
abs(opath))
221+
val doCompile =
222+
!opath.exists() || cpath.lastModified() >= opath.lastModified()
223+
224+
if (doCompile && run(command) != 0) {
225+
sys.error(s"Failed to compile $cpath")
226+
}
227+
opath
228+
}
229+
230+
val archivePath = cwd / s"lib$libname.a"
231+
val archive = Seq("ar", "cr", abs(archivePath)) ++ opaths.map(abs)
232+
val doArchive =
233+
opaths.map(_.lastModified).max >= archivePath.lastModified()
234+
235+
if (doArchive && run(archive) != 0) {
236+
sys.error(s"Failed to create archive $archivePath")
237+
}
238+
239+
(Test / compile).value
240+
}
241+
)

docs/src/paradox/cli.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Generating Bindings with the CLI
2+
3+
Statically linked executables are provided with each release for Linux and macOS. Head over to the [releases page] to download the latest version for your platform.
4+
5+
[releases page]: https://github.com/kornilova-l/scala-native-bindgen/releases
6+
7+
@@@ note
8+
9+
In the following we assume you have renamed the downloaded `scala-native-bindgen-$PLATFORM` file to `scala-native-bindgen`.
10+
11+
@@@
12+
13+
## Command Line Usage
14+
15+
When generating bindings with the CLI, you need to specify the header file and provide the name of the created bindings using the `--name` option:
16+
17+
```sh
18+
scala-native-bindgen --name fnmatch /usr/include/fnmatch.h --
19+
```
20+
21+
When running the CLI it will also yield warnings along with the translation. To keep only the bindings please redirect the output to a file like this:
22+
23+
```sh
24+
scala-native-bindgen --name fnmatch /usr/include/fnmatch.h -- > fnmatch.scala
25+
```
26+
27+
By default it is assumed that you want to link with a library based on the name option.
28+
In case the name of the library to link with is different from the binding name provide the library name using `--link`:
29+
30+
```sh
31+
scala-native-bindgen --name zlib --link z /usr/include/zlib.h --
32+
```
33+
34+
If the binding does not require any linking, pass `--no-link`:
35+
36+
```sh
37+
scala-native-bindgen --name fnmatch --no-link /usr/include/fnmatch.h --
38+
```
39+
40+
For libraries that require linking you m
41+
42+
## Options
43+
44+
The generated bindings can be configured using the different options and it is also possible to pass arguments directly to the Clang compiler using the `--extra-arg*` options.
45+
46+
| Option | Description
47+
|----------------------|---------------------------------------------------------------------------------|
48+
| `--link` | Library to link with, e.g. `--link` uv for libuv.
49+
| `--no-link` | Library does not require linking.
50+
| `--name` | Scala object name that contains bindings. Default value set to library name.
51+
| `--package` | Package name of generated Scala file.
52+
| `--exclude-prefix` | Functions and unused typedefs will be removed if their names have given prefix.
53+
| `--binding-config` | Path to a config file that contains the information about bindings that should be reused. See @ref:[Integrating Bindings](integrating-bindings.md) for more information.
54+
| `--extra-arg` | Additional argument to append to the compiler command line.
55+
| `--extra-arg-before` | Additional argument to prepend to the compiler command line.

docs/src/paradox/command-line-usage/index.md

-26
This file was deleted.

docs/src/paradox/obtaining-bindgen/cmake.md docs/src/paradox/contrib/cmake.md

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Building with `CMake`
1+
# Building the executable with `CMake`
22

33
Building `scala-native-bindgen` requires the following tools and libraries:
44

@@ -40,16 +40,6 @@ equivalent, e.g. to compile with debug symbols the following are the same:
4040
```sh
4141
cmake -DCMAKE_CXX_FLAGS=-g ..
4242
CXXFLAGS=-g cmake ..
43-
```
44-
45-
## Testing
46-
47-
The tests assume that the above instructions for building `scala-native-bindgen` from source
48-
has been followed.
49-
50-
```sh
51-
cd tests
52-
sbt test
5343
```
5444

5545
[CMake]: https://cmake.org/

docs/src/paradox/obtaining-bindgen/docker-compose.md docs/src/paradox/contrib/docker-compose.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Building with `docker-compose`
1+
# Building the executable with `docker-compose`
22

33
You can use [docker-compose] to build and test the program:
44

@@ -11,4 +11,5 @@ docker-compose run --rm ubuntu-18.04-llvm-6.0 ./script/test.sh
1111
docker-compose run --rm ubuntu-18.04-llvm-6.0 \
1212
bindgen/target/scala-native-bindgen --name union tests/samples/Union.h --
1313
```
14+
1415
[docker-compose]: https://docs.docker.com/compose/
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../CONTRIBUTING.md

docs/src/paradox/contrib/index.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Contributor's Guide
2+
3+
Contributions to the project is very welcome. This section provides more
4+
information about how to build and contribute to the project.
5+
6+
@@ toc
7+
8+
@@@ index
9+
10+
* [guidelines](guidelines.md)
11+
* [cmake](cmake.md)
12+
* [docker-compose](docker-compose.md)
13+
* [releasing](releasing.md)
14+
15+
@@@

docs/src/paradox/contrib/releasing.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Release Workflow
2+
3+
First build the `scala-native-bindgen` executable for both macOS and
4+
Linux:
5+
6+
```sh
7+
scripts/prepare-release.sh
8+
```
9+
10+
You should now have `scala-native-bindgen-linux` and
11+
`scala-native-bindgen-darwin` if you ran the script on a macOS machine.
12+
13+
Then release version `x.y.z` by running:
14+
15+
```sh
16+
sbt -Dproject.version=x.y.z release
17+
```
18+
19+
Finally, upload the `scala-native-bindgen-linux` and
20+
`scala-native-bindgen-darwin` executables to the release page at:
21+
<https://github.com/kornilova-l/scala-native-bindgen/releases/tag/vx.y.z>

0 commit comments

Comments
 (0)