diff --git a/build/create_binaries/README.md b/build/create_binaries/README.md index 70dbb26db..b6ade3e69 100644 --- a/build/create_binaries/README.md +++ b/build/create_binaries/README.md @@ -1,6 +1,5 @@ # Create Binaries - Build the app with: ``` @@ -12,3 +11,9 @@ Build the shared library with: ``` pixi run build-libribasim ``` + +Build both with: + +``` +pixi run build +``` diff --git a/build/create_binaries/build.jl b/build/create_binaries/build.jl new file mode 100644 index 000000000..9c27c6e84 --- /dev/null +++ b/build/create_binaries/build.jl @@ -0,0 +1,20 @@ +using create_binaries + +""" +Build the Ribasim CLI, libribasim, or both, using PackageCompiler. +Run from the command line with: + + julia --project build.jl --app --lib +""" +function main(ARGS) + # change directory to this script's location + cd(@__DIR__) + + if "--app" in ARGS + build_app() + elseif "--lib" in ARGS + build_lib() + end +end + +main(ARGS) diff --git a/build/create_binaries/create_app.jl b/build/create_binaries/create_app.jl deleted file mode 100644 index 72e6a7262..000000000 --- a/build/create_binaries/create_app.jl +++ /dev/null @@ -1,39 +0,0 @@ -using PackageCompiler -using TOML -using LibGit2 - -include("strip_cldr.jl") - -# change directory to this script's location -cd(@__DIR__) - -project_dir = "../ribasim_cli" -license_file = "../../LICENSE" -output_dir = "ribasim_cli" -git_repo = "../.." - -create_app( - project_dir, - output_dir; - # map from binary name to julia function name - executables = ["ribasim" => "julia_main"], - precompile_execution_file = "precompile.jl", - include_lazy_artifacts = true, - force = true, -) - -include("add_metadata.jl") -add_metadata(project_dir, license_file, output_dir, git_repo) - -# On Windows, write ribasim.cmd in the output_dir, that starts ribasim.exe. -# Since the bin dir contains a julia.exe and many DLLs that you may not want in your path, -# with this script you can put output_dir in your path instead. -if Sys.iswindows() - cmd = raw""" - @echo off - "%~dp0bin\ribasim.exe" %* - """ - open(normpath(output_dir, "ribasim.cmd"); write = true) do io - print(io, cmd) - end -end diff --git a/build/create_binaries/create_lib.jl b/build/create_binaries/create_lib.jl deleted file mode 100644 index 0b8364099..000000000 --- a/build/create_binaries/create_lib.jl +++ /dev/null @@ -1,24 +0,0 @@ -using PackageCompiler -using TOML -using LibGit2 - -include("strip_cldr.jl") - -cd(@__DIR__) - -project_dir = "../libribasim" -license_file = "../../LICENSE" -output_dir = "libribasim" -git_repo = "../.." - -create_library( - project_dir, - output_dir; - lib_name = "libribasim", - precompile_execution_file = "precompile.jl", - include_lazy_artifacts = true, - force = true, -) - -include("add_metadata.jl") -add_metadata(project_dir, license_file, output_dir, git_repo) diff --git a/build/create_binaries/add_metadata.jl b/build/create_binaries/src/add_metadata.jl similarity index 100% rename from build/create_binaries/add_metadata.jl rename to build/create_binaries/src/add_metadata.jl diff --git a/build/create_binaries/src/create_app.jl b/build/create_binaries/src/create_app.jl new file mode 100644 index 000000000..056dc5b7b --- /dev/null +++ b/build/create_binaries/src/create_app.jl @@ -0,0 +1,32 @@ +"Build the Ribasim CLI using PackageCompiler" +function build_app() + project_dir = "../ribasim_cli" + license_file = "../../LICENSE" + output_dir = "ribasim_cli" + git_repo = "../.." + + create_app( + project_dir, + output_dir; + # map from binary name to julia function name + executables = ["ribasim" => "julia_main"], + precompile_execution_file = "precompile.jl", + include_lazy_artifacts = true, + force = true, + ) + + add_metadata(project_dir, license_file, output_dir, git_repo) + + # On Windows, write ribasim.cmd in the output_dir, that starts ribasim.exe. + # Since the bin dir contains a julia.exe and many DLLs that you may not want in your path, + # with this script you can put output_dir in your path instead. + if Sys.iswindows() + cmd = raw""" + @echo off + "%~dp0bin\ribasim.exe" %* + """ + open(normpath(output_dir, "ribasim.cmd"); write = true) do io + print(io, cmd) + end + end +end diff --git a/build/create_binaries/src/create_binaries.jl b/build/create_binaries/src/create_binaries.jl index 12701a5c9..dcd00dba1 100644 --- a/build/create_binaries/src/create_binaries.jl +++ b/build/create_binaries/src/create_binaries.jl @@ -1,4 +1,15 @@ -__precompile__(false) +module create_binaries -# This is not really a package, but needs to be set up like one so we can run -# `dev build/create_binaries` from the root project. +using Artifacts +using PackageCompiler +using TOML +using LibGit2 + +export build_app, build_lib + +include("strip_cldr.jl") +include("add_metadata.jl") +include("create_app.jl") +include("create_lib.jl") + +end diff --git a/build/create_binaries/src/create_lib.jl b/build/create_binaries/src/create_lib.jl new file mode 100644 index 000000000..8e8b01a31 --- /dev/null +++ b/build/create_binaries/src/create_lib.jl @@ -0,0 +1,18 @@ +"Build libribasim using PackageCompiler" +function build_lib() + project_dir = "../libribasim" + license_file = "../../LICENSE" + output_dir = "libribasim" + git_repo = "../.." + + create_library( + project_dir, + output_dir; + lib_name = "libribasim", + precompile_execution_file = "precompile.jl", + include_lazy_artifacts = true, + force = true, + ) + + add_metadata(project_dir, license_file, output_dir, git_repo) +end diff --git a/build/create_binaries/src/strip_cldr.jl b/build/create_binaries/src/strip_cldr.jl new file mode 100644 index 000000000..d63765403 --- /dev/null +++ b/build/create_binaries/src/strip_cldr.jl @@ -0,0 +1,25 @@ +""" +The cldr artifact has such long paths that it errors on Windows unless long paths are enabled. +Also the artifact has many files and is over 300 MB, while we only need a single small file. +This modifies the artifact to remove everything except the file we need. +Since the artifact is only used on Windows, only strip do it there. +This needs exactly TimeZones 1.13.0, which is fixed in the Project.toml. +https://github.com/JuliaTime/TimeZones.jl/issues/373 +""" +function strip_cldr() + if Sys.iswindows() + # Get the artifact directory and the file path we need to keep + hash = Base.SHA1("40b35727ea0aff9a9f28b7454004b68849caf67b") + @assert artifact_exists(hash) + artifact_dir = artifact_path(hash) + keep_file = + normpath(artifact_dir, "cldr-release-43-1/common/supplemental/windowsZones.xml") + @assert isfile(keep_file) + + # Read the file into memory, empty the artifact dir, and write the file back + keep_file_content = read(keep_file) + rm(artifact_dir; recursive = true) + mkpath(dirname(keep_file)) + write(keep_file, keep_file_content) + end +end diff --git a/build/create_binaries/strip_cldr.jl b/build/create_binaries/strip_cldr.jl deleted file mode 100644 index fbded628c..000000000 --- a/build/create_binaries/strip_cldr.jl +++ /dev/null @@ -1,24 +0,0 @@ -# The cldr artifact has such long paths that it errors on Windows unless long paths are enabled. -# Also the artifact has many files and is over 300 MB, while we only need a single small file. -# This modifies the artifact to remove everything except the file we need. -# Since the artifact is only used on Windows, only strip do it there. -# This needs exactly TimeZones 1.13.0, which is fixed in the Project.toml. -# https://github.com/JuliaTime/TimeZones.jl/issues/373 - -using Artifacts - -if Sys.iswindows() - # Get the artifact directory and the file path we need to keep - hash = Base.SHA1("40b35727ea0aff9a9f28b7454004b68849caf67b") - @assert artifact_exists(hash) - artifact_dir = artifact_path(hash) - keep_file = - normpath(artifact_dir, "cldr-release-43-1/common/supplemental/windowsZones.xml") - @assert isfile(keep_file) - - # Read the file into memory, empty the artifact dir, and write the file back - keep_file_content = read(keep_file) - rm(artifact_dir; recursive = true) - mkpath(dirname(keep_file)) - write(keep_file, keep_file_content) -end diff --git a/pixi.toml b/pixi.toml index 4568e3fad..196b1c12b 100644 --- a/pixi.toml +++ b/pixi.toml @@ -53,15 +53,18 @@ lint = { depends_on = [ "mypy-ribasim-api", ] } # Build -build-ribasim-cli = { cmd = "cd build/create_binaries && julia --project create_app.jl", depends_on = [ +build-ribasim-cli = { cmd = "cd build/create_binaries && julia --project build.jl --app", depends_on = [ "generate-testmodels", "instantiate-julia", ] } -build-libribasim = { cmd = "cd build/create_binaries && julia --project create_lib.jl", depends_on = [ +build-libribasim = { cmd = "cd build/create_binaries && julia --project build.jl --lib", depends_on = [ + "generate-testmodels", + "instantiate-julia", +] } +build = { "cmd" = "julia --project build.jl --app --lib", cwd = "build/create_binaries", depends_on = [ "generate-testmodels", "instantiate-julia", ] } -build = { depends_on = ["build-ribasim-cli", "build-libribasim"] } # Test test-ribasim-python = "pytest --numprocesses=auto python/ribasim/tests" test-ribasim-api = "pytest --basetemp=python/ribasim_api/tests/temp --junitxml=report.xml python/ribasim_api/tests"