Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be resilient with Bazel-built transitive dependencies duplicates #318

Merged
merged 2 commits into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ test_suite(
"//configure_with_bazel_transitive:test",
"//simple_make:test_lib",
"//cmake_hello_world_variant:test_hello_world",
"//cmake_synthetic:test_bazel_transitive_deps",
],
)

Expand Down
23 changes: 23 additions & 0 deletions examples/cmake_synthetic/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@rules_foreign_cc//tools/build_defs:cmake.bzl", "cmake_external")
load("@bazel_skylib//rules:build_test.bzl", "build_test")

generate_crosstool = select({
"@bazel_tools//src/conditions:windows": True,
Expand Down Expand Up @@ -31,6 +32,28 @@ cmake_external(
deps = [":liba"],
)

cmake_external(
name = "lib_with_duplicate_transitive_bazel_deps",
lib_name = "libc",
cmake_options = ["-GNinja"],
cache_entries = {
"LIBA_DIR": "$$EXT_BUILD_DEPS$$",
"LIBB_DIR": "$$EXT_BUILD_DEPS$$",
},
generate_crosstool_file = generate_crosstool,
lib_source = "//cmake_synthetic/libc:c_srcs",
make_commands = [
"ninja",
"ninja install",
],
deps = ["//cmake_synthetic/liba:lib_a_bazel", "//cmake_synthetic/libb:lib_b_bazel"],
)

build_test(
name = "test_bazel_transitive_deps",
targets = [":lib_with_duplicate_transitive_bazel_deps"],
)

cc_test(
name = "test_libs",
srcs = [
Expand Down
9 changes: 9 additions & 0 deletions examples/cmake_synthetic/libb/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@ filegroup(
srcs = glob(["**"]),
visibility = ["//visibility:public"],
)

cc_library(
name = "lib_b_bazel",
srcs = ["src/libb.cpp"],
hdrs = ["src/libb.h"],
includes = ["src"],
visibility = ["//visibility:public"],
deps = ["//cmake_synthetic/liba:lib_a_bazel"],
)
5 changes: 5 additions & 0 deletions examples/cmake_synthetic/libc/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
filegroup(
name = "c_srcs",
srcs = glob(["**"]),
visibility = ["//visibility:public"],
)
5 changes: 5 additions & 0 deletions examples/cmake_synthetic/libc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cmake_minimum_required(VERSION 2.8.4)

project(libc)

add_subdirectory(src)
28 changes: 28 additions & 0 deletions examples/cmake_synthetic/libc/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
cmake_minimum_required(VERSION 2.8.4)

set(libc_SRC libc.cpp libc.h)

add_library(libc_static STATIC ${libc_SRC})
set_target_properties(libc_static PROPERTIES OUTPUT_NAME "libc")
IF (WIN32)
set_target_properties(libc_static PROPERTIES ARCHIVE_OUTPUT_NAME "libc")
ELSE()
set_target_properties(libc_static PROPERTIES ARCHIVE_OUTPUT_NAME "c")
ENDIF()

#find_package(LIBA NAMES alib liba libliba liba++)
#find_package(LIBB NAMES blib libb liblibb)

# LIBA_INCLUDE_DIRS is not set, so giving the path relative to liba_config.cmake
# would be happy to improve that
target_link_libraries(libc_static PUBLIC ${LIBA_DIR}/lib/liblib_a_bazel.a)
target_include_directories(libc_static PUBLIC ${LIBA_DIR}/include)

target_link_libraries(libc_static PUBLIC ${LIBB_DIR}/lib/liblib_b_bazel.a)
target_include_directories(libc_static PUBLIC ${LIBB_DIR}/include)

set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)

install(TARGETS libc_static ARCHIVE DESTINATION lib)

install(FILES libc.h DESTINATION include)
5 changes: 5 additions & 0 deletions examples/cmake_synthetic/libc/src/libc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "libc.h"

std::string hello_libc(void) {
return hello_liba() + " " + hello_libb() + " Hello from LIBC!";
}
11 changes: 11 additions & 0 deletions examples/cmake_synthetic/libc/src/libc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef LIBC_H_
#define LIBC_H_ (1)

#include <stdio.h>
#include <string>
#include "liba.h"
#include "libb.h"

std::string hello_libc(void);

#endif
3 changes: 3 additions & 0 deletions tools/build_defs/framework.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,9 @@ def _copy_deps_and_tools(files):
return lines

def _symlink_contents_to_dir(dir_name, files_list):
# It is possible that some duplicate libraries will be passed as inputs
# to cmake_external or configure_make. Filter duplicates out here.
files_list = collections.uniq(files_list)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment here?

"It is possible that some duplicate libraries will be passed as inputs to cmake_external or configure_make. Filter duplicates out here."

if len(files_list) == 0:
return []
lines = ["##mkdirs## $$EXT_BUILD_DEPS$$/" + dir_name]
Expand Down