File tree Expand file tree Collapse file tree 6 files changed +81
-1
lines changed
examples/cmake_with_target Expand file tree Collapse file tree 6 files changed +81
-1
lines changed Original file line number Diff line number Diff line change 48
48
- " -//cmake_defines/..."
49
49
- " -//cmake_hello_world_lib/..."
50
50
- " -//cmake_with_data/..."
51
+ - " -//cmake_with_target/..."
51
52
- " -//cmake_working_dir/..."
52
53
- " -//configure_with_bazel_transitive/..."
53
54
- " -//make_simple/..."
Original file line number Diff line number Diff line change
1
+ load ("@rules_foreign_cc//foreign_cc:defs.bzl" , "cmake" )
2
+
3
+ filegroup (
4
+ name = "srcs" ,
5
+ srcs = glob (["src/**" ]),
6
+ visibility = ["//visibility:public" ],
7
+ )
8
+
9
+ # Building all targets with the `srcs` CMake project will fail.
10
+ # this example demonstrates how users can avoid unnecessary targets
11
+ cmake (
12
+ name = "cmake_with_target" ,
13
+ build_args = [
14
+ "--verbose" ,
15
+ "--" , # <- Pass remaining options to the native tool.
16
+ "-j 1" ,
17
+ ],
18
+ install_args = ["--component working" ],
19
+ lib_source = ":srcs" ,
20
+ out_static_libs = select ({
21
+ "@platforms//os:windows" : [
22
+ "working_a.lib" ,
23
+ "working_b.lib"
24
+ ],
25
+ "//conditions:default" : [
26
+ "libworking_a.a" ,
27
+ "libworking_b.a" ,
28
+ ],
29
+ }),
30
+ targets = [
31
+ "working_a" ,
32
+ "working_b" ,
33
+ ],
34
+ )
Original file line number Diff line number Diff line change
1
+ cmake_minimum_required (VERSION 3.18.1 )
2
+
3
+ project (cmake_with_target_example )
4
+
5
+ set (LIB_SRC lib.cpp lib.h )
6
+
7
+ add_library (working_a STATIC ${LIB_SRC} )
8
+ target_compile_definitions (working_a PRIVATE TRIGGER_ERROR=0 )
9
+
10
+ add_library (working_b STATIC ${LIB_SRC} )
11
+ target_compile_definitions (working_b PRIVATE TRIGGER_ERROR=0 )
12
+
13
+ add_library (broken STATIC ${LIB_SRC} )
14
+ target_compile_definitions (broken PRIVATE TRIGGER_ERROR=1 )
15
+
16
+ install (TARGETS working_a working_b COMPONENT working ARCHIVE DESTINATION lib )
17
+ install (TARGETS broken COMPONENT broken ARCHIVE DESTINATION lib )
18
+ install (FILES lib.h DESTINATION include )
Original file line number Diff line number Diff line change
1
+ #include " lib.h"
2
+
3
+ #include < iostream>
4
+
5
+ #ifndef TRIGGER_ERROR
6
+ #error This value must be defined
7
+ #endif
8
+
9
+ #if TRIGGER_ERROR
10
+ #error This error intentionally prevents this library from compiling
11
+ #endif
12
+
13
+ void hello_world ()
14
+ {
15
+ std::cout << " Hello world!" << std::endl;
16
+ }
Original file line number Diff line number Diff line change
1
+ #ifndef LIB_H_
2
+ #define LIB_H_
3
+
4
+ void hello_world ();
5
+
6
+ #endif
Original file line number Diff line number Diff line change @@ -89,9 +89,14 @@ def _create_configure_script(configureParameters):
89
89
# Generate commands for all the targets, ensuring there's
90
90
# always at least 1 call to the default target.
91
91
for target in ctx .attr .targets or ["" ]:
92
+
93
+ # There's no need to use the `--target` argument for an empty/"all" target
94
+ if target :
95
+ target = "--target '{}'" .format (target )
96
+
92
97
# Note that even though directory is always passed, the
93
98
# following arguments can take precedence.
94
- cmake_commands .append ("{cmake} --build {dir} --config {config} {args } {target }" .format (
99
+ cmake_commands .append ("{cmake} --build {dir} --config {config} {target } {args }" .format (
95
100
cmake = attrs .cmake_path ,
96
101
dir = "." ,
97
102
args = build_args ,
You can’t perform that action at this time.
0 commit comments