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

Correctly render package private defs when package has parts #257

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 9 additions & 7 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -622,21 +622,23 @@ def collectBindings(headersPath: File) = {
)
.toMap

headerSpec.toSeq.map { case (header, name) =>
val PREF = "//!bindgen"
val contents = IO
.readLines(header)
def directives(pref: String, lines: List[String]): List[String] =
lines
.map(_.trim)
.filter(_.startsWith(PREF))
.map(_.stripPrefix(PREF).trim)
.filter(_.startsWith(pref))
.map(_.stripPrefix(pref).trim)
.flatMap(_.split(" "))
.map(_.trim)

headerSpec.toSeq.map { case (header, name) =>
val lines = IO.readLines(header)
val contents = directives("//!bindgen:", lines)
val pkg = directives("//!bindgen-package:", lines).headOption
val multiFileFlag = "--multi-file"
val isMultiFile = contents.contains(multiFileFlag)

Binding
.builder(header, s"lib_test_$name")
.builder(header, pkg.getOrElse(s"lib_test_$name"))
.addCImport(s"$name.h")
.withBindgenArguments(contents.filterNot(_ == multiFileFlag))
.withMultiFile(isMultiFile)
Expand Down
8 changes: 5 additions & 3 deletions modules/bindgen/src/main/scala/render/binding.scala
Original file line number Diff line number Diff line change
Expand Up @@ -396,16 +396,17 @@ private def renderEnumerations(
if mode == RenderMode.Objects then out.appendLine("object predef:")
nestIf(mode == RenderMode.Objects) {

val safePackageName = packageName.split('.').last
val predefSigned = s"""
|private[$packageName] trait CEnum[T](using eq: T =:= Int):
|private[$safePackageName] trait CEnum[T](using eq: T =:= Int):
| given Tag[T] = Tag.Int.asInstanceOf[Tag[T]]
| extension (inline t: T)
| inline def int: CInt = eq.apply(t)
| inline def value: CInt = eq.apply(t)
""".stripMargin.trim.linesIterator

val predefUnsigned = s"""
|private[$packageName] trait CEnumU[T](using eq: T =:= UInt):
|private[${safePackageName}] trait CEnumU[T](using eq: T =:= UInt):
| given Tag[T] = Tag.UInt.asInstanceOf[Tag[T]]
| extension (inline t: T)
| inline def int: CInt = eq.apply(t).toInt
Expand Down Expand Up @@ -463,8 +464,9 @@ private def renderScalaFunctions(
summon[Config].linkName.foreach { l =>
out.append(s"""@link("$l")""")
}
val safePackageName = packageName.split('.').last
out.appendLine(
s"\n@extern\nprivate[$packageName] object extern_functions:"
s"\n@extern\nprivate[$safePackageName] object extern_functions:"
)
nest {
if renderMode == RenderMode.Objects then
Expand Down
3 changes: 2 additions & 1 deletion modules/bindgen/src/main/scala/render/function.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def renderFunction(f: GeneratedFunction.ScalaFunction, line: Appender)(using
val flatArguments = f.arguments.flatten

val access =
if f.public then "" else s"private[${summon[Context].packageName.value}] "
if f.public then ""
else s"private[${summon[Context].packageName.value.split('.').last}] "

if f.public then f.meta.foreach(renderComment(line, _))
f.body match
Expand Down
2 changes: 1 addition & 1 deletion modules/export-tests/library/src/main/c/export_basics.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//!bindgen --export
//!bindgen: --export

extern int myscalalib_exports(int i, int r);

Expand Down
2 changes: 1 addition & 1 deletion modules/tests/src/test/resources/scala-native/multi_file.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//!bindgen --multi-file
//!bindgen: --multi-file

union Test {
int x;
Expand Down
3 changes: 3 additions & 0 deletions modules/tests/src/test/resources/scala-native/package_tests.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
int help_me(char c) {
return 25 + c;
}
3 changes: 3 additions & 0 deletions modules/tests/src/test/resources/scala-native/package_tests.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//!bindgen-package: lib_test.mty.bla

int help_me(char c);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//!bindgen: --multi-file
//!bindgen-package: lib_test.mty.bla.multi

int help_me_multi(char c);
Loading