Skip to content

Commit d425656

Browse files
authored
Fixed cmake build and target arguments (#573)
1 parent ede2c80 commit d425656

File tree

6 files changed

+81
-1
lines changed

6 files changed

+81
-1
lines changed

.bazelci/config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ tasks:
4848
- "-//cmake_defines/..."
4949
- "-//cmake_hello_world_lib/..."
5050
- "-//cmake_with_data/..."
51+
- "-//cmake_with_target/..."
5152
- "-//cmake_working_dir/..."
5253
- "-//configure_with_bazel_transitive/..."
5354
- "-//make_simple/..."
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
}

examples/cmake_with_target/src/lib.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef LIB_H_
2+
#define LIB_H_
3+
4+
void hello_world();
5+
6+
#endif

foreign_cc/cmake.bzl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,14 @@ def _create_configure_script(configureParameters):
8989
# Generate commands for all the targets, ensuring there's
9090
# always at least 1 call to the default target.
9191
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+
9297
# Note that even though directory is always passed, the
9398
# 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(
95100
cmake = attrs.cmake_path,
96101
dir = ".",
97102
args = build_args,

0 commit comments

Comments
 (0)