llvm-project/clang/tools/clang-fuzzer/CMakeLists.txt

123 lines
3.1 KiB
CMake
Raw Normal View History

set(LLVM_LINK_COMPONENTS ${LLVM_TARGETS_TO_BUILD} FuzzMutate)
set(CXX_FLAGS_NOFUZZ ${CMAKE_CXX_FLAGS})
set(DUMMY_MAIN DummyClangFuzzer.cpp)
if(LLVM_LIB_FUZZING_ENGINE)
unset(DUMMY_MAIN)
elseif(LLVM_USE_SANITIZE_COVERAGE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=fuzzer")
set(CXX_FLAGS_NOFUZZ "${CXX_FLAGS_NOFUZZ} -fsanitize=fuzzer-no-link")
unset(DUMMY_MAIN)
endif()
# Needed by LLVM's CMake checks because this file defines multiple targets.
set(LLVM_OPTIONAL_SOURCES
ClangFuzzer.cpp
DummyClangFuzzer.cpp
ExampleClangProtoFuzzer.cpp
ExampleClangLoopProtoFuzzer.cpp
ExampleClangLLVMProtoFuzzer.cpp
)
if(CLANG_ENABLE_PROTO_FUZZER)
# Create protobuf .h and .cc files, and put them in a library for use by
# clang-proto-fuzzer components.
find_package(Protobuf REQUIRED)
add_definitions(-DGOOGLE_PROTOBUF_NO_RTTI)
include_directories(${PROTOBUF_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_BINARY_DIR})
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS cxx_proto.proto)
protobuf_generate_cpp(LOOP_PROTO_SRCS LOOP_PROTO_HDRS cxx_loop_proto.proto)
set(LLVM_OPTIONAL_SOURCES ${LLVM_OPTIONAL_SOURCES} ${PROTO_SRCS})
add_clang_library(clangCXXProto
${PROTO_SRCS}
${PROTO_HDRS}
LINK_LIBS
${PROTOBUF_LIBRARIES}
)
add_clang_library(clangCXXLoopProto
${LOOP_PROTO_SRCS}
${LOOP_PROTO_HDRS}
LINK_LIBS
${PROTOBUF_LIBRARIES}
)
# Build and include libprotobuf-mutator
include(ProtobufMutator)
include_directories(${ProtobufMutator_INCLUDE_DIRS})
# Build the protobuf->C++ translation library and driver.
add_clang_subdirectory(proto-to-cxx)
# Build the protobuf->LLVM IR translation library and driver.
add_clang_subdirectory(proto-to-llvm)
# Build the fuzzer initialization library.
add_clang_subdirectory(fuzzer-initialize)
# Build the protobuf fuzzer
add_clang_executable(clang-proto-fuzzer
${DUMMY_MAIN}
ExampleClangProtoFuzzer.cpp
)
# Build the loop protobuf fuzzer
add_clang_executable(clang-loop-proto-fuzzer
${DUMMY_MAIN}
ExampleClangLoopProtoFuzzer.cpp
)
# Build the llvm protobuf fuzzer
add_clang_executable(clang-llvm-proto-fuzzer
${DUMMY_MAIN}
ExampleClangLLVMProtoFuzzer.cpp
)
set(COMMON_PROTO_FUZZ_LIBRARIES
${ProtobufMutator_LIBRARIES}
${PROTOBUF_LIBRARIES}
${LLVM_LIB_FUZZING_ENGINE}
clangFuzzerInitialize
)
target_link_libraries(clang-proto-fuzzer
PRIVATE
${COMMON_PROTO_FUZZ_LIBRARIES}
clangHandleCXX
clangCXXProto
clangProtoToCXX
)
target_link_libraries(clang-loop-proto-fuzzer
PRIVATE
${COMMON_PROTO_FUZZ_LIBRARIES}
clangHandleCXX
clangCXXLoopProto
clangLoopProtoToCXX
)
target_link_libraries(clang-llvm-proto-fuzzer
PRIVATE
${COMMON_PROTO_FUZZ_LIBRARIES}
clangHandleLLVM
clangCXXLoopProto
clangLoopProtoToLLVM
)
endif()
add_clang_subdirectory(handle-cxx)
add_clang_subdirectory(handle-llvm)
add_clang_executable(clang-fuzzer
EXCLUDE_FROM_ALL
${DUMMY_MAIN}
ClangFuzzer.cpp
)
target_link_libraries(clang-fuzzer
[CMake] Use PRIVATE in target_link_libraries for executables We currently use target_link_libraries without an explicit scope specifier (INTERFACE, PRIVATE or PUBLIC) when linking executables. Dependencies added in this way apply to both the target and its dependencies, i.e. they become part of the executable's link interface and are transitive. Transitive dependencies generally don't make sense for executables, since you wouldn't normally be linking against an executable. This also causes issues for generating install export files when using LLVM_DISTRIBUTION_COMPONENTS. For example, clang has a lot of LLVM library dependencies, which are currently added as interface dependencies. If clang is in the distribution components but the LLVM libraries it depends on aren't (which is a perfectly legitimate use case if the LLVM libraries are being built static and there are therefore no run-time dependencies on them), CMake will complain about the LLVM libraries not being in export set when attempting to generate the install export file for clang. This is reasonable behavior on CMake's part, and the right thing is for LLVM's build system to explicitly use PRIVATE dependencies for executables. Unfortunately, CMake doesn't allow you to mix and match the keyword and non-keyword target_link_libraries signatures for a single target; i.e., if a single call to target_link_libraries for a particular target uses one of the INTERFACE, PRIVATE, or PUBLIC keywords, all other calls must also be updated to use those keywords. This means we must do this change in a single shot. I also fully expect to have missed some instances; I tested by enabling all the projects in the monorepo (except dragonegg), and configuring both with and without shared libraries, on both Darwin and Linux, but I'm planning to rely on the buildbots for other configurations (since it should be pretty easy to fix those). Even after this change, we still have a lot of target_link_libraries calls that don't specify a scope keyword, mostly for shared libraries. I'm thinking about addressing those in a follow-up, but that's a separate change IMO. Differential Revision: https://reviews.llvm.org/D40823 llvm-svn: 319840
2017-12-06 05:49:56 +08:00
PRIVATE
${LLVM_LIB_FUZZING_ENGINE}
clangHandleCXX
)