Skip to content

Commit 8a86fdf

Browse files
committed
fix ninja and sync simple examples
ninja and make fit roughly into the same category of build system, but we were only setting up the C build variables for make. This finishes the ninja implementation to match make (more or less) and then syncs the simple examples to match. The simple examples that don't already have compiler integrations (i.e. make and ninja) now use the same wrapper, which loads data from the configured toolchain instead of calling PATH binaries.
1 parent d3ece2a commit 8a86fdf

File tree

19 files changed

+208
-100
lines changed

19 files changed

+208
-100
lines changed

.bazelci/config.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,6 @@ tasks:
170170
- "-//cmake_synthetic/..."
171171
# TODO: Fix `error LNK2019: unresolved external symbol hello_func`
172172
- "-//cmake_hello_world_lib/shared/..."
173-
# TODO: Fix `CreateProcess failed: The system cannot find the file specified.`
174-
- "-//ninja_simple/..."
175173
# TODO: The use of Visual Studio generator targets are broken. These should
176174
# be re-enabled pending a resolution to
177175
# https://github.com/bazelbuild/continuous-integration/issues/1204

examples/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
load("@rules_python//python/pip_install:requirements.bzl", "compile_pip_requirements")
22

3+
exports_files([
4+
"cc_wrapper.sh",
5+
])
6+
37
test_suite(
48
name = "third_party_examples_linux_tests",
59
tags = ["manual"],

examples/cc_wrapper.sh

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/env bash
2+
3+
# This wrapper exists to provide a simplified way to wrap the compiler to do
4+
# the simple examples (make/ninja) that don't have the capability of doing
5+
# their own compiler setup. This exists _just_ to demonstrate and test
6+
# functionality for these simple builds, and if you're doing something real,
7+
# for your sake, use a full cross-platform build system like cmake and _don't_
8+
# use this script :)
9+
10+
set -euo pipefail
11+
set -x
12+
13+
die() {
14+
echo "$*" >&2
15+
exit 42
16+
}
17+
18+
ensure_absolute() {
19+
local path="$1"
20+
21+
# Tool paths may be provided with surrounding quotes on Windows.
22+
path="${path#\"}"
23+
path="${path%\"}"
24+
25+
case "$path" in
26+
/*|[A-Za-z]:[\\/]*)
27+
# already absolute (maybe with a drive path)
28+
echo "$path"
29+
;;
30+
*)
31+
echo "$EXT_BUILD_ROOT/$path"
32+
;;
33+
esac
34+
}
35+
36+
compile() {
37+
if [[ -z "${CXX:-}" ]]; then
38+
die "CXX is not set"
39+
fi
40+
41+
local cxx
42+
cxx="$(ensure_absolute "$CXX")"
43+
44+
case "$cxx" in
45+
*/cl.exe)
46+
"$cxx" /c "$1" ${CXXFLAGS:-} "/Fo$2"
47+
;;
48+
*)
49+
"$cxx" -c "$1" ${CXXFLAGS:-} -o "$2"
50+
;;
51+
esac
52+
}
53+
54+
static_link() {
55+
if [[ -z "${AR:-}" ]]; then
56+
die "AR is not set"
57+
fi
58+
59+
local ar
60+
ar="$(ensure_absolute "$AR")"
61+
62+
case "$ar" in
63+
*/lib.exe)
64+
"$ar" ${ARFLAGS:-/nologo} "/OUT:$2" "$1"
65+
;;
66+
*/libtool)
67+
# Note that this is darwin libtool, not gnu.
68+
"$ar" ${ARFLAGS:-} -static -o "$2" "$1"
69+
;;
70+
*)
71+
"$ar" ${ARFLAGS:-rcsD} "$2" "$1"
72+
;;
73+
esac
74+
}
75+
76+
if [[ -z "${EXT_BUILD_ROOT:-}" ]]; then
77+
die "EXT_BUILD_ROOT is not set"
78+
fi
79+
80+
if [[ "$#" -ne 3 ]]; then
81+
die "Usage: $0 <verb> <input> <output>"
82+
fi
83+
84+
verb=$1
85+
input=$2
86+
output=$3
87+
88+
case "$verb" in
89+
compile) compile "$input" "$output" ;;
90+
static_link) static_link "$input" "$output" ;;
91+
*) die "unknown verb: $verb" ;;
92+
esac

examples/make_simple/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ load("@rules_foreign_cc//foreign_cc:defs.bzl", "make")
44
make(
55
name = "make_lib",
66
build_data = [
7-
"//make_simple/code:cxx_wrapper.sh",
7+
"//:cc_wrapper.sh",
88
"README.md",
99
],
1010
copts = [
1111
"-DREQUIRED_DEFINE",
1212
],
1313
env = {
14-
"CXX_WRAPPER": "$(execpath //make_simple/code:cxx_wrapper.sh)",
14+
"CC_WRAPPER": "$(execpath //:cc_wrapper.sh)",
1515
"README_DIR": "$$(dirname $(execpath README.md))",
1616
},
1717
lib_source = "//make_simple/code:srcs",

examples/make_simple/code/BUILD.bazel

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
exports_files(["cxx_wrapper.sh"])
2-
31
filegroup(
42
name = "srcs",
53
srcs = [

examples/make_simple/code/Makefile

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
BUILD_DIR := build-out
22

3-
UNAME := $(shell uname)
4-
5-
ifneq (,$(findstring NT, $(UNAME)))
6-
# If Windows
7-
CXXFLAGS := $(CXXFLAGS) /MD
8-
else
9-
CXXFLAGS := $(CXXFLAGS) -fPIC
10-
endif
11-
123
default all $(BUILD_DIR)/lib/liba.a: liba.cpp liba.h
134
rm -rf $(BUILD_DIR)/lib
145
mkdir -p $(BUILD_DIR)/lib
15-
$(CXX_WRAPPER) $(CXXFLAGS) -o $(BUILD_DIR)/lib/liba.o -c liba.cpp
16-
ar rcs $(BUILD_DIR)/lib/liba.a $(BUILD_DIR)/lib/liba.o
6+
bash $(CC_WRAPPER) compile liba.cpp $(BUILD_DIR)/lib/liba.o
7+
bash $(CC_WRAPPER) static_link $(BUILD_DIR)/lib/liba.o $(BUILD_DIR)/lib/liba.a
178

189
.PHONY: all
1910

examples/make_simple/code/cxx_wrapper.sh

Lines changed: 0 additions & 11 deletions
This file was deleted.

examples/meson_simple/BUILD.bazel

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,6 @@ load("@rules_shell//shell:sh_test.bzl", "sh_test")
44

55
meson(
66
name = "meson_lib",
7-
build_data = ["//meson_simple/code:clang_wrapper.sh"],
8-
env = select({
9-
"@platforms//os:windows": {
10-
"CLANG_WRAPPER": "$(execpath //meson_simple/code:clang_wrapper.sh)",
11-
"CXX_FLAGS": "/MD",
12-
},
13-
"//conditions:default": {
14-
"CLANG_WRAPPER": "$(execpath //meson_simple/code:clang_wrapper.sh)",
15-
"CXX_FLAGS": "-fPIC",
16-
},
17-
}),
187
lib_source = "//meson_simple/code:srcs",
198
out_static_libs = ["liba.a"],
209
)

examples/meson_simple/code/BUILD.bazel

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
exports_files(["clang_wrapper.sh"])
2-
31
filegroup(
42
name = "srcs",
53
srcs = [

examples/meson_simple/code/clang_wrapper.sh

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)