Skip to content

Commit

Permalink
Add cmake package (#144)
Browse files Browse the repository at this point in the history
* Simplify how sysroot is set in `std.toolchain()`

* Don't set `$CPATH` in `std.toolchain()`

This causes issues with `--sysroot` (e.g. with how CMake detects include
paths)

* Rename `std/runnable_tools.bri` module to `runtime_utils.bri`

* Update runtime utils

* Update toolchain to wrap gcc with new `brioche-cc` binary

* Add CMake package

* Disable tracing in CMake build

* Generate CMake patch from forked git repo

* Update `std.runtimeUtils()`

* Export `std.runtimeUtils()`

* Update `std.toolchain()`, `curl`, and `openssl` to set CMake project root vars

* Update CMake patches

* Update internal runtime utils

* Update CMake env vars in `std.toolchain()`, `curl`, and `cmake`

* Update `std` lockfile

* Add `cmake.cmakeBuild()` function

* Add `test` export for `cmake`

* Bump CMake version to v3.31.1
  • Loading branch information
kylewlacy authored Dec 2, 2024
1 parent 3b9b281 commit f95a3dc
Show file tree
Hide file tree
Showing 12 changed files with 228 additions and 174 deletions.
15 changes: 15 additions & 0 deletions packages/cmake/brioche.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

195 changes: 195 additions & 0 deletions packages/cmake/project.bri
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
import * as std from "std";
import { gitCheckout } from "git";
import openssl from "openssl";
import curl from "curl";

export const project = {
name: "cmake",
version: "3.31.1",
};

function patch(): std.Recipe<std.File> {
// Instead of directly using a fork of CMake, we instead grab the branch
// `brioche-patches`, diff it from `base/brioche-patches`, then apply it
// to whichever version of CMake we're building. This may make it easier
// to stay up-to-date, but it's not clear if this is the right approach...

return std.recipeFn(() => {
const base = gitCheckout(
Brioche.gitRef({
repository: "https://github.com/brioche-dev/CMake.git",
ref: "base/brioche-patches",
}),
);
const patched = gitCheckout(
Brioche.gitRef({
repository: "https://github.com/brioche-dev/CMake.git",
ref: "brioche-patches",
}),
);

return std.runBash`
diff -ru base patched > "$BRIOCHE_OUTPUT" || true
`
.workDir(
std.directory({
base: base.remove(".git"),
patched: patched.remove(".git"),
}),
)
.toFile();
});
}

const source = (() => {
let source = Brioche.download(
`https://github.com/Kitware/CMake/releases/download/v${project.version}/cmake-${project.version}.tar.gz`,
)
.unarchive("tar", "gzip")
.peel();

// Apply patch file
source = std
.process({
command: "patch",
args: ["-p1", "-i", patch(), "-d", std.outputPath],
outputScaffold: source,
dependencies: [std.tools()],
})
.toDirectory();

return source;
})();

export default function cmake(): std.Recipe<std.Directory> {
let cmake = std.runBash`
./bootstrap \\
--prefix=/ \\
--system-curl \\
--parallel=16
make
make install DESTDIR="$BRIOCHE_OUTPUT"
`
.workDir(source)
.dependencies(std.toolchain(), openssl(), curl())
.toDirectory();

cmake = cmake.insert("libexec/cmake/runtime-utils", std.runtimeUtils());
cmake = cmake.insert(
"libexec/cmake/brioche-packer",
std.symlink({ target: "runtime-utils/bin/brioche-packer" }),
);

cmake = std.setEnv(cmake, {
CPATH: { append: [{ path: "include" }] },
LIBRARY_PATH: { append: [{ path: "lib" }] },
PKG_CONFIG_PATH: { append: [{ path: "lib/pkgconfig" }] },
});

return std.withRunnableLink(cmake, "bin/cmake");
}

export interface CMakeBuildInstallOptions {
source: std.AsyncRecipe<std.Directory>;
dependencies?: std.AsyncRecipe<std.Directory>[];
config?: string;
env?: Record<string, std.ProcessTemplateLike>;
set?: Record<string, CMakeVariable>;
runnable?: string;
}

export type CMakeVariable =
| string
| { type?: CMakeVariableType; value: string };

export type CMakeVariableType =
| "BOOL"
| "FILEPATH"
| "PATH"
| "STRING"
| "INTERNAL";

/**
* Generate and build a CMake project.
*
* A project buildsystem will be generated out-of-tree from the source project,
* and will be built and installed automatically with the chosen build
* generator.
*
* If the result includes a `lib64/`, the symlink `lib/` will be added
* automatically to follow the standard Brioche conventions.
*
* ## Options
*
* - `source`: The CMake project to build.
* - `dependencies`: Optionally add dependencies to the build. Most projects
* will want to include `std.toolchain()` or a similar toolchain.
* - `env`: Optionally set environment variables for the build.
* - `set`: Optionally set CMake cache variables during the build, as if
* by passing `-D...`.
* - `runnable`: Optionally set a path to the binary to run by default
* (e.g. `bin/foo`).
*/
export function cmakeBuild(
options: CMakeBuildInstallOptions,
): std.Recipe<std.Directory> {
const { source, dependencies = [], config = "Release", set = {} } = options;

const env: Record<string, string> = {};
for (const [name, variable] of Object.entries(set)) {
std.assert(
/^[a-zA-Z0-9\-_]+$/.test(name),
`invalid CMake variable name: ${name}`,
);

const value = typeof variable === "object" ? variable.value : variable;
const type = typeof variable === "object" ? variable.type : undefined;

env[`cmake_value_${name}`] = value;
env[`cmake_type_${name}`] = type ?? "";
}

let result = std.runBash`
export LIB="$LIBRARY_PATH"
cmake_args=()
for name in $cmake_set_names; do
var_cmake_value="cmake_value_$name"
var_cmake_type="cmake_type_$name"
if [ -n "\${!var_cmake_type}" ]; then
cmake_args+=("-D\${name}:\${!var_cmake_type}=\${!var_cmake_value}")
else
cmake_args+=("-D\${name}=\${!var_cmake_value}")
fi
done
cmake "$source" "\${cmake_args[@]}"
cmake --build . --config "$config"
cmake --install . --prefix="$BRIOCHE_OUTPUT"
if [ -d "$BRIOCHE_OUTPUT/lib64" ]; then
ln -s lib64 "$BRIOCHE_OUTPUT/lib"
fi
`
.dependencies(...dependencies, cmake())
.env({
...options.env,
source,
config,
cmake_set_names: Object.keys(set).join(" "),
...env,
})
.toDirectory();

if (options.runnable != null) {
result = std.withRunnableLink(result, options.runnable);
}

return result;
}

export function test() {
return std.runBash`
cmake --version | tee "$BRIOCHE_OUTPUT"
`.dependencies(cmake());
}
2 changes: 2 additions & 0 deletions packages/curl/project.bri
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export default function (): std.Recipe<std.Directory> {
CPATH: { append: [{ path: "include" }] },
LIBRARY_PATH: { append: [{ path: "lib" }] },
PKG_CONFIG_PATH: { append: [{ path: "lib/pkgconfig" }] },
CMAKE_PREFIX_PATH: { append: [{ path: "." }] },
CURL_ROOT: { fallback: { path: "." } },
});

return std.withRunnableLink(curl, "bin/curl");
Expand Down
2 changes: 2 additions & 0 deletions packages/openssl/project.bri
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export default function openssl(): std.Recipe<std.Directory> {
CPATH: { append: [{ path: "include" }] },
LIBRARY_PATH: { append: [{ path: "lib" }] },
PKG_CONFIG_PATH: { append: [{ path: "lib/pkgconfig" }] },
CMAKE_PREFIX_PATH: { append: [{ path: "." }] },
OPENSSL_ROOT_DIR: { fallback: { path: "." } },
});

return std.withRunnableLink(openssl, "bin/openssl");
Expand Down
4 changes: 2 additions & 2 deletions packages/std/brioche.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/std/extra/autopack.bri
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as std from "/core";
import { toolchain } from "/toolchain";
import { runtimeUtils } from "/runnable_tools.bri";
import { runtimeUtils } from "/runtime_utils.bri";

/**
* "Autopack" one or more files in a directory. Generally, this is useful
Expand Down
2 changes: 1 addition & 1 deletion packages/std/extra/runnable.bri
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as std from "/core";
import {
type RunnableTemplate,
makeRunnableExecutable,
} from "/runnable_tools.bri";
} from "/runtime_utils.bri";

export type WithRunnable = std.Recipe<std.Directory> & WithRunnableUtils;

Expand Down
1 change: 1 addition & 0 deletions packages/std/project.bri
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { toolchain } from "./toolchain";
export * from "./core";
export * from "./extra";
export * from "./toolchain";
export { runtimeUtils } from "./runtime_utils.bri";

export const project = {
name: "std",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as std from "/core";

export function runtimeUtils(): std.Recipe<std.Directory> {
return Brioche.download(
"https://development-content.brioche.dev/github.com/brioche-dev/brioche-runtime-utils/commits/4815f007ab625a42a0e72820fdd6e154b9d5d1c6/x86_64-linux/brioche-runtime-utils.tar.zstd",
"https://development-content.brioche.dev/github.com/brioche-dev/brioche-runtime-utils/commits/b44f9b743898b74faa79ee1a599440a2e2e6f295/x86_64-linux/brioche-runtime-utils.tar.zstd",
).unarchive("tar", "zstd");
}

Expand Down
Loading

0 comments on commit f95a3dc

Please sign in to comment.