Skip to content

Commit

Permalink
fix: resolve path issues in CMakeLinker (#3)
Browse files Browse the repository at this point in the history
* dev: add NodePath bindings

* fix: dirname corner case

* refactor: CMakeLinker path handling
  • Loading branch information
seven-mile authored Sep 18, 2024
1 parent eace44d commit 34b55f8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
10 changes: 10 additions & 0 deletions packages/cosmo/src/main/scala/cosmo/Cosmo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,16 @@ object NodeFs extends js.Object {
def unlinkSync(path: String): Unit = js.native
}

@js.native
@JSImport("path", JSImport.Namespace)
object NodePath extends js.Object {
def join(paths: String*): String = js.native
def resolve(path: String*): String = js.native
def relative(from: String, to: String): String = js.native
def dirname(path: String): String = js.native
def basename(path: String): String = js.native
}

final class SpawnSyncResult extends js.Object {
var pid: Int = _
var status: js.UndefOr[Int] = _
Expand Down
48 changes: 22 additions & 26 deletions packages/cosmo/src/main/scala/cosmo/linker/CmakeLinker.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package cosmo.linker

import scala.scalajs.js

import cosmo.system._
import cosmo.{Package, Transpiler, FileId}
import cosmo.{Package, Transpiler, FileId, NodePath}
import cosmo.{debugln, logln}

import scala.scalajs.js

class CmakeLinker(system: CosmoSystem) extends Linker {
lazy val buildDir = "cmake-build-relwithdebinfo";
// is windows
lazy val isWin = js.Dynamic.global.process.platform.asInstanceOf[String] == "win32"
// is windows
lazy val isWin =
js.Dynamic.global.process.platform.asInstanceOf[String] == "win32"

def writeIfDiff(path: String, content: String): Unit =
cosmo.linker.writeIfDiff(system, path, content)
Expand Down Expand Up @@ -56,38 +57,27 @@ target_link_libraries(cosmo_json INTERFACE cosmo_std)
t: cosmo.Transpiler,
relReleaseDir: String,
): Option[String] = {
def inRelPath(path: String) = NodePath.resolve(relReleaseDir, path)

val start = System.currentTimeMillis()
val src = system.readFile(path)
val generated = t.transpile(src).map { case (content, noCore) =>
var suf = if (noCore) "/lang" else ""
s"""#include <cosmo/std/src/prelude${suf}.h> // IWYU pragma: keep\n\n${content}"""
}

var nlJsonDir = system
.absPath("target/cosmo/externals/json/single_include")
.replace("\\", "\\\\")

var releaseDir =
system.absPath(relReleaseDir).replace("\\", "\\\\")

var includeFlags = js.Array(
s"/I$nlJsonDir",
s"/I$releaseDir",
)

val fileName = path.substring(0, path.length - 4)
val destDir = "package-less"
val destPath = destDir + "/" + fileName + ".cc"
val dirPath = destPath.substring(0, destPath.lastIndexOf("/"))
val destPath =
NodePath.join("package-less", path.stripSuffix(".cos") + ".cc")
val dirPath = NodePath.dirname(destPath)

generated.flatMap { content =>
system.mkdir(releaseDir + "/" + dirPath)
system.writeFile(releaseDir + "/" + destPath, content)
system.mkdir(inRelPath(dirPath))
system.writeFile(inRelPath(destPath), content)

writeIfDiff(
s"$relReleaseDir/packageOnly.cmake",
inRelPath("packageOnly.cmake"),
s"""
add_executable(cosmo-user-prog $destPath)
add_executable(cosmo-user-prog ${replaceSep(destPath)})
target_link_libraries(cosmo-user-prog PUBLIC cosmo_std cosmo_json)
""",
);
Expand Down Expand Up @@ -118,7 +108,9 @@ target_link_libraries(cosmo-user-prog PUBLIC cosmo_std cosmo_json)
val execSuffix = if (isWin) ".exe" else ""

// todo: this only works with ninja
Some(s"$buildDir/$relReleaseDir/$target$execSuffix")
val programPath = s"$buildDir/$relReleaseDir/$target$execSuffix"
logln(s"programPath: $programPath")
Some(NodePath.resolve(programPath))
}

debugln(s"Compilation time: ${System.currentTimeMillis() - start}ms")
Expand All @@ -134,4 +126,8 @@ target_link_libraries(cosmo-user-prog PUBLIC cosmo_std cosmo_json)
}
}
}

def replaceSep(path: String): String = {
if (isWin) path.replace('\\', '/') else path
}
}

0 comments on commit 34b55f8

Please sign in to comment.