From 3f21a03bc3522c6a9c96159bb56e7b126d5cd8ba Mon Sep 17 00:00:00 2001 From: Steven Johnson Date: Mon, 8 Jul 2019 17:18:41 -0700 Subject: [PATCH 01/14] Revamp CMake to build both static and shared libraries --- .gitignore | 2 +- .travis.yml | 8 +- CMakeLists.txt | 25 +-- apps/linear_algebra/tests/CMakeLists.txt | 2 +- halide.cmake | 48 ++++-- src/CMakeLists.txt | 194 +++++++++++++---------- test/CMakeLists.txt | 9 +- tools/build_halide_h.cpp | 6 - tutorial/CMakeLists.txt | 6 +- util/CMakeLists.txt | 1 + 10 files changed, 171 insertions(+), 130 deletions(-) diff --git a/.gitignore b/.gitignore index 6afc31c386fe..905c6333a93d 100644 --- a/.gitignore +++ b/.gitignore @@ -8,7 +8,7 @@ python_bindings/bin/* build-64/* build-ios/* build-osx/* -cmake_build/* +cmake_build*/* */build/* tmp/* doc/* diff --git a/.travis.yml b/.travis.yml index d11d719aa9e3..96aab79b347a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,9 +5,10 @@ compiler: - gcc env: global: - # The Travis Ubuntu Xenial environment we run in currently promises 2 cores, - # so running lengthy make steps with -j2 is almost certainly a win. - - MAKEFLAGS=-j2 + # The Travis Ubuntu Xenial environment we run in currently promises 2 cores. + # We'll run at -j4 on the assumption that there's enough I/O bound work + # for that to be profitable. + - MAKEFLAGS=-j4 matrix: # Configurations # @@ -20,6 +21,7 @@ env: # Note that gcc5.4 is the default install on Travis Xenial, so we'll just use that. - LLVM_VERSION=8.0.0 BUILD_SYSTEM=MAKE - LLVM_VERSION=8.0.0 BUILD_SYSTEM=CMAKE HALIDE_SHARED_LIBRARY=1 + - LLVM_VERSION=8.0.0 BUILD_SYSTEM=CMAKE HALIDE_SHARED_LIBRARY=0 # # llvm7 prebuilts are cranky on Travis and give flaky failures; we just skipped them # in Trusty and continue to skip them in Xenial. diff --git a/CMakeLists.txt b/CMakeLists.txt index 62bfa401b249..6ab329006de5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ project(Halide) -cmake_minimum_required(VERSION 3.2) +cmake_minimum_required(VERSION 3.3) set(HALIDE_BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}") @@ -162,15 +162,15 @@ option(TARGET_OPENCL "Include OpenCL-C target" ON) option(TARGET_OPENGL "Include OpenGL/GLSL target" ON) option(TARGET_OPENGLCOMPUTE "Include OpenGLCompute target" ON) option(TARGET_D3D12COMPUTE "Include Direct3D 12 Compute target" ON) -option(HALIDE_SHARED_LIBRARY "Build as a shared library" ON) +option(HALIDE_SHARED_LIBRARY "Build tests+apps using shared library (vs static)" ON) option(HALIDE_ENABLE_RTTI "Enable RTTI" ${LLVM_ENABLE_RTTI}) option(HALIDE_ENABLE_EXCEPTIONS "Enable exceptions" ${LLVM_ENABLE_EH}) option(HALIDE_USE_CODEMODEL_LARGE "Use the Large LLVM codemodel" OFF) if (HALIDE_SHARED_LIBRARY) - set(HALIDE_LIBRARY_TYPE SHARED) + message(STATUS "Build tests+apps using Halide shared library") else() - set(HALIDE_LIBRARY_TYPE STATIC) + message(STATUS "Build tests+apps using Halide static library") endif() if (HALIDE_ENABLE_RTTI AND NOT LLVM_ENABLE_RTTI) @@ -195,9 +195,8 @@ function(halide_project name folder) target_compile_options("${name}" PUBLIC "-fno-rtti") endif() endif() - target_link_libraries("${name}" PRIVATE Halide ${CMAKE_DL_LIBS} ${CMAKE_THREAD_LIBS_INIT}) - target_include_directories("${name}" PRIVATE "${HALIDE_BASE_DIR}/src") - target_include_directories("${name}" PRIVATE "${HALIDE_BASE_DIR}/tools") + target_link_libraries("${name}" PRIVATE ${HALIDE_COMPILER_LIB} ${CMAKE_DL_LIBS} ${CMAKE_THREAD_LIBS_INIT}) + target_include_directories("${name}" PRIVATE "${HALIDE_INCLUDE_DIR}" "${HALIDE_TOOLS_DIR}") set_target_properties("${name}" PROPERTIES FOLDER "${folder}") if (MSVC) target_link_libraries("${name}" PRIVATE Kernel32) @@ -267,7 +266,11 @@ endif() # to specific values, rather than relying on HALIDE_DISTRIB_DIR to be set correctly. set(HALIDE_INCLUDE_DIR "${CMAKE_BINARY_DIR}/include") set(HALIDE_TOOLS_DIR "${HALIDE_BASE_DIR}/tools") -set(HALIDE_COMPILER_LIB Halide) +if (HALIDE_SHARED_LIBRARY) + set(HALIDE_COMPILER_LIB HalideShared) +else() + set(HALIDE_COMPILER_LIB HalideStatic) +endif() set(HALIDE_DISTRIB_DIR "/bad-path") include(halide.cmake) @@ -330,7 +333,6 @@ function(add_halide_test TARGET) endforeach() set(BUILD_NAME "${TARGET}") - # add_custom_target("${BUILD_NAME}" DEPENDS "${TARGET}") set_target_properties("${BUILD_NAME}" PROPERTIES EXCLUDE_FROM_ALL TRUE) add_dependencies(build_tests "${BUILD_NAME}") @@ -431,8 +433,7 @@ install(FILES "${CMAKE_BINARY_DIR}/include/Halide.h" "${CMAKE_BINARY_DIR}/include/HalideBuffer.h" ${FILES} - DESTINATION include - ) + DESTINATION include) install(DIRECTORY tutorial DESTINATION . @@ -465,7 +466,7 @@ file(GLOB FILES "${HALIDE_BASE_DIR}/*.md") install(FILES ${FILES} DESTINATION .) -# ---- CMake +# ---- halide.cmake file(GLOB FILES "${HALIDE_BASE_DIR}/*.cmake") install(FILES ${FILES} DESTINATION .) diff --git a/apps/linear_algebra/tests/CMakeLists.txt b/apps/linear_algebra/tests/CMakeLists.txt index d7def1e7c554..cc02eb0a4f96 100644 --- a/apps/linear_algebra/tests/CMakeLists.txt +++ b/apps/linear_algebra/tests/CMakeLists.txt @@ -20,6 +20,6 @@ target_link_libraries(test_halide_blas PRIVATE halide_blas cblas # XXX fragile - Halide + ${HALIDE_COMPILER_LIB} ) diff --git a/halide.cmake b/halide.cmake index e623e77474bb..aae33d4b66cf 100644 --- a/halide.cmake +++ b/halide.cmake @@ -1,6 +1,9 @@ include(CMakeParseArguments) -cmake_minimum_required(VERSION 3.1.3) +cmake_minimum_required(VERSION 3.3) + +# Allow VISIBILITY_INLINES_HIDDEN to work for static libraries as well as shared. Requires CMake 3.3+. +cmake_policy(SET CMP0063 NEW) # ----------------------- Public Functions. # These are all documented in README_cmake.md. @@ -49,8 +52,8 @@ function(halide_generator NAME) # at least one source file, and this is the cheapest one we're going to have. add_executable("${NAME}_binary" "${HALIDE_TOOLS_DIR}/GenGen.cpp") _halide_set_cxx_options("${NAME}_binary") - target_include_directories("${NAME}_binary" PRIVATE "${HALIDE_TOOLS_DIR}") - target_link_libraries("${NAME}_binary" PRIVATE ${HALIDE_COMPILER_LIB} ${HALIDE_SYSTEM_LIBS} ${CMAKE_DL_LIBS} ${CMAKE_THREAD_LIBS_INIT}) + target_include_directories("${NAME}_binary" PRIVATE "${HALIDE_INCLUDE_DIR}" "${HALIDE_TOOLS_DIR}") + target_link_libraries("${NAME}_binary" PRIVATE ${HALIDE_SYSTEM_LIBS} ${CMAKE_DL_LIBS} ${CMAKE_THREAD_LIBS_INIT}) if (MSVC) target_link_libraries("${NAME}_binary" PRIVATE Kernel32) endif() @@ -71,11 +74,18 @@ function(halide_generator NAME) endforeach() # Ensure that Halide.h is built prior to any Generator add_dependencies("${GENLIB}" ${HALIDE_COMPILER_LIB}) - - _halide_get_static_library_actual_path(${GENLIB} GENLIB_ACTUAL_PATH) _halide_force_link_library("${NAME}_binary" "${GENLIB}") endif() + if ("${HALIDE_LIBRARY_TYPE}" STREQUAL "STATIC") + # Getting link order correct for static libraries is nearly impossible in CMake; + # to avoid flakiness, always link libHalide via --whole-archive + # when in static-library mode. + _halide_force_link_library("${NAME}_binary" "${HALIDE_COMPILER_LIB}") + else() + target_link_libraries("${NAME}_binary" PRIVATE ${HALIDE_COMPILER_LIB}) + endif() + _halide_genfiles_dir(${BASENAME} GENFILES_DIR) set(STUB_HDR "${GENFILES_DIR}/${BASENAME}.stub.h") set(GENERATOR_EXEC_ARGS "-g" "${args_GENERATOR_NAME}" "-o" "${GENFILES_DIR}" "-e" "cpp_stub" "-n" "${BASENAME}") @@ -89,15 +99,14 @@ function(halide_generator NAME) set_property(TARGET "${NAME}_stub_gen" PROPERTY _HALIDE_GENERATOR_NAME "${args_GENERATOR_NAME}") if("${SRCSLEN}" GREATER 0) + _halide_get_static_library_actual_path(${GENLIB} GENLIB_ACTUAL_PATH) add_library("${NAME}" STATIC IMPORTED) - set_target_properties("${NAME}" PROPERTIES - IMPORTED_LOCATION "${GENLIB_ACTUAL_PATH}") + set_target_properties("${NAME}" PROPERTIES IMPORTED_LOCATION "${GENLIB_ACTUAL_PATH}") else() add_library("${NAME}" INTERFACE) endif() add_dependencies("${NAME}" "${NAME}_stub_gen") - set_target_properties("${NAME}" PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES "${GENFILES_DIR}") + set_target_properties("${NAME}" PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${GENFILES_DIR}") endfunction() # Use a Generator target to emit a code library. @@ -621,7 +630,8 @@ function(_halide_add_exec_generator_target EXEC_TARGET) add_custom_command( OUTPUT ${args_OUTPUTS} DEPENDS ${args_GENERATOR_BINARY} - COMMAND ${CMAKE_COMMAND} -E echo Running $ ${args_GENERATOR_ARGS} + # Reduce noise during build; uncomment for debugging + # COMMAND ${CMAKE_COMMAND} -E echo Running $ ${args_GENERATOR_ARGS} COMMAND ${RUN_WITHOUT_LEAKCHECK} $ ${args_GENERATOR_ARGS} COMMENT "${EXTRA_OUTPUTS_COMMENT}" ) @@ -629,9 +639,11 @@ function(_halide_add_exec_generator_target EXEC_TARGET) add_custom_command( OUTPUT ${args_OUTPUTS} DEPENDS ${args_GENERATOR_BINARY} - COMMAND ${CMAKE_COMMAND} -E echo copying $ to "$" + # Reduce noise during build; uncomment for debugging + # COMMAND ${CMAKE_COMMAND} -E echo copying $ to "$" COMMAND ${CMAKE_COMMAND} -E copy_if_different $ "$" - COMMAND ${CMAKE_COMMAND} -E echo Running $ ${args_GENERATOR_ARGS} + # Reduce noise during build; uncomment for debugging + # COMMAND ${CMAKE_COMMAND} -E echo Running $ ${args_GENERATOR_ARGS} COMMAND ${RUN_WITHOUT_LEAKCHECK} $ ${args_GENERATOR_ARGS} COMMENT "${EXTRA_OUTPUTS_COMMENT}" ) @@ -646,14 +658,20 @@ function(_halide_force_link_library NAME LIB) # (or the equivalent), to ensure that the Generator-registration code # isn't omitted. Sadly, there's no portable way to do this, so we do some # special-casing here: - _halide_get_static_library_actual_path(${LIB} LIB_ACTUAL_PATH) if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") + _halide_get_static_library_actual_path("${LIB}" LIB_ACTUAL_PATH) target_link_libraries("${NAME}" PRIVATE "${LIB}") - set_target_properties("${NAME}" PROPERTIES LINK_FLAGS -Wl,-force_load,${GENLIB_ACTUAL_PATH}) + # Append to LINK_FLAGS, since we may call this multiple times + get_property(flags TARGET "${NAME}" PROPERTY LINK_FLAGS) + set(flags "${flags} -Wl,-force_load,${LIB_ACTUAL_PATH}") + set_target_properties("${NAME}" PROPERTIES LINK_FLAGS ${flags}) elseif(MSVC) # Note that this requires VS2015 R2+ target_link_libraries("${NAME}" PRIVATE "${LIB}") - set_target_properties("${NAME}" PROPERTIES LINK_FLAGS "/WHOLEARCHIVE:${LIB}.lib") + # Append to LINK_FLAGS, since we may call this multiple times + get_property(flags TARGET "${NAME}" PROPERTY LINK_FLAGS) + set(flags "${flags} /WHOLEARCHIVE:${LIB}.lib") + set_target_properties("${NAME}" PROPERTIES LINK_FLAGS ${flags}) else() # Assume Linux or similar target_link_libraries("${NAME}" PRIVATE -Wl,--whole-archive "${LIB}" -Wl,-no-whole-archive) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f8f9a3bf48d9..bfd32a93b149 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -195,11 +195,11 @@ function(add_runtime_modules TARGET_RUNTIME_FILES TARGET_RUNTIME_DIR) COMMENT "${LL} -> ${BC}") add_custom_command(OUTPUT "${INITMOD_D}" - DEPENDS "${BC_D}" + DEPENDS "${BC_D}" binary2cpp COMMAND binary2cpp "halide_internal_initmod_${i}_${j}_debug" < "${BC_D}" > "${INITMOD_D}" COMMENT "${BC_D} -> ${INITMOD_D}") add_custom_command(OUTPUT "${INITMOD}" - DEPENDS "${BC}" + DEPENDS "${BC}" binary2cpp COMMAND binary2cpp "halide_internal_initmod_${i}_${j}" < "${BC}" > "${INITMOD}" COMMENT "${BC} -> ${INITMOD}") list(APPEND INITIAL_MODULES ${INITMOD}) @@ -222,7 +222,7 @@ foreach (i ${RUNTIME_LL} ) COMMAND "${LLVM_AS}" "${LL}" -o "${BC}" COMMENT "${LL} -> ${BC}") add_custom_command(OUTPUT "${INITMOD}" - DEPENDS "${BC}" + DEPENDS "${BC}" binary2cpp COMMAND binary2cpp "halide_internal_initmod_${i}_ll" < "${BC}" > "${INITMOD}" COMMENT "${BC} -> ${INITMOD}") list(APPEND INITIAL_MODULES "${INITMOD}") @@ -230,6 +230,7 @@ endforeach() foreach (i ${RUNTIME_BC} ) set(INITMOD "${INITMOD_PREFIX}ptx_${i}.cpp") add_custom_command(OUTPUT "${INITMOD}" + DEPENDS binary2cpp COMMAND binary2cpp "halide_internal_initmod_ptx_${i}_ll" < "${NATIVE_RUNTIME_DIR}nvidia_libdevice_bitcode/libdevice.${i}.10.bc" > "${INITMOD}" COMMENT "Building initial module ptx_${i}..." VERBATIM) @@ -237,7 +238,7 @@ foreach (i ${RUNTIME_BC} ) endforeach() add_custom_command(OUTPUT "${INITMOD_PREFIX}inlined_c.cpp" - DEPENDS "${NATIVE_RUNTIME_DIR}buffer_t.cpp" + DEPENDS "${NATIVE_RUNTIME_DIR}buffer_t.cpp" binary2cpp COMMAND binary2cpp "halide_internal_initmod_inlined_c" < "${NATIVE_RUNTIME_DIR}buffer_t.cpp" > "${INITMOD_PREFIX}inlined_c.cpp" COMMENT "buffer_t.cpp -> ${INITMOD_PREFIX}inlined_c.cpp") list(APPEND INITIAL_MODULES "${INITMOD_PREFIX}inlined_c.cpp") @@ -259,7 +260,7 @@ set(RUNTIME_HEADER_FILES foreach (i ${RUNTIME_HEADER_FILES}) string(REPLACE "." "_" SYM_NAME "${i}") add_custom_command(OUTPUT "${INITMOD_PREFIX}${SYM_NAME}.cpp" - DEPENDS "${NATIVE_RUNTIME_DIR}${i}" + DEPENDS "${NATIVE_RUNTIME_DIR}${i}" binary2cpp COMMAND binary2cpp "halide_internal_runtime_header_${SYM_NAME}" < "${NATIVE_RUNTIME_DIR}${i}" > "${INITMOD_PREFIX}${SYM_NAME}.cpp" COMMENT "${i} -> ${INITMOD_PREFIX}${SYM_NAME}.cpp") list(APPEND INITIAL_MODULES "${INITMOD_PREFIX}${SYM_NAME}.cpp") @@ -444,7 +445,7 @@ foreach (i ${RUNTIME_HEADER_FILES}) endforeach() # Keep this list sorted in alphabetical order. -add_library(Halide ${HALIDE_LIBRARY_TYPE} +set(SOURCE_FILES AddImageChecks.cpp AddParameterChecks.cpp AlignLoads.cpp @@ -609,165 +610,186 @@ add_library(Halide ${HALIDE_LIBRARY_TYPE} WasmExecutor.cpp WrapCalls.cpp WrapExternStages.cpp - ${HEADER_FILES} - ${INITIAL_MODULES} ) -# We could expose the /MP flag to all targets, but that might end up saturating the build -# since multiple MSBuild projects might get built in parallel, each of which compiling their -# source files in parallel; the Halide library itself is a "knot" point of the build graph, -# so compiling its files in parallel should not oversubscribe the system -target_compile_options(Halide PUBLIC $<$:/MP>) - -# Define Halide_SHARED or Halide_STATIC depending on library type -target_compile_definitions(Halide PRIVATE "-DHalide_${HALIDE_LIBRARY_TYPE}") -# Ensure that these tools are build first -add_dependencies(Halide - binary2cpp - build_halide_h - HalideIncludes -) +# The list of -D flags to pass when compiling +set(HALIDE_COMPILE_DEFINITIONS ) # List of LLVM Components required # This list will be appended to depending on the targets we need to support # See the output of ``llvm-config --components`` for a list of possible components -set(LLVM_COMPONENTS mcjit;bitwriter;linker) -list(APPEND LLVM_COMPONENTS passes) - -# Set definitions and compiler flags - -# Note when PUBLIC or INTERFACE scope is used in target_compile_* then targets -# that link against the Halide library inherit those options and definitions -target_include_directories(Halide PRIVATE ${LLVM_INCLUDE_DIRS}) -target_include_directories(Halide INTERFACE "${CMAKE_BINARY_DIR}/include") +set(LLVM_COMPONENTS mcjit;bitwriter;linker;passes) # TODO: For targets we can link against even fewer libraries by specifying # only the components we **REALLY** need (e.g. x86asmprinter;x86codegen rather than x86) if (TARGET_X86) - target_compile_definitions(Halide PRIVATE "-DWITH_X86") + list(APPEND HALIDE_COMPILE_DEFINITIONS "-DWITH_X86") list(APPEND LLVM_COMPONENTS X86) endif() if (TARGET_ARM) - target_compile_definitions(Halide PRIVATE "-DWITH_ARM") + list(APPEND HALIDE_COMPILE_DEFINITIONS "-DWITH_ARM") list(APPEND LLVM_COMPONENTS ARM) endif() if (TARGET_AARCH64) - target_compile_definitions(Halide PRIVATE "-DWITH_AARCH64") + list(APPEND HALIDE_COMPILE_DEFINITIONS "-DWITH_AARCH64") list(APPEND LLVM_COMPONENTS AArch64) endif() if (TARGET_HEXAGON) - target_compile_definitions(Halide PRIVATE "-DWITH_HEXAGON") + list(APPEND HALIDE_COMPILE_DEFINITIONS "-DWITH_HEXAGON") list(APPEND LLVM_COMPONENTS Hexagon) endif() if (TARGET_MIPS) - target_compile_definitions(Halide PRIVATE "-DWITH_MIPS") + list(APPEND HALIDE_COMPILE_DEFINITIONS "-DWITH_MIPS") list(APPEND LLVM_COMPONENTS Mips) endif() if (TARGET_POWERPC) - target_compile_definitions(Halide PRIVATE "-DWITH_POWERPC") + list(APPEND HALIDE_COMPILE_DEFINITIONS "-DWITH_POWERPC") list(APPEND LLVM_COMPONENTS PowerPC) endif() if (TARGET_WEBASSEMBLY) - target_compile_definitions(Halide PRIVATE "-DWITH_WEBASSEMBLY") + list(APPEND HALIDE_COMPILE_DEFINITIONS "-DWITH_WEBASSEMBLY") list(APPEND LLVM_COMPONENTS WebAssembly) endif() if (TARGET_PTX) - target_compile_definitions(Halide PRIVATE "-DWITH_PTX") + list(APPEND HALIDE_COMPILE_DEFINITIONS "-DWITH_PTX") list(APPEND LLVM_COMPONENTS NVPTX) endif() if (TARGET_AMDGPU) - target_compile_definitions(Halide PRIVATE "-DWITH_AMDGPU") + list(APPEND HALIDE_COMPILE_DEFINITIONS "-DWITH_AMDGPU") list(APPEND LLVM_COMPONENTS AMDGPU) endif() if (TARGET_RISCV) - target_compile_definitions(Halide PRIVATE "-DWITH_RISCV") + list(APPEND HALIDE_COMPILE_DEFINITIONS "-DWITH_RISCV") list(APPEND LLVM_COMPONENTS RISCV) endif() if (TARGET_OPENCL) - target_compile_definitions(Halide PRIVATE "-DWITH_OPENCL") + list(APPEND HALIDE_COMPILE_DEFINITIONS "-DWITH_OPENCL") endif() if (TARGET_OPENGL) - target_compile_definitions(Halide PRIVATE "-DWITH_OPENGL") + list(APPEND HALIDE_COMPILE_DEFINITIONS "-DWITH_OPENGL") endif() if (TARGET_METAL) - target_compile_definitions(Halide PRIVATE "-DWITH_METAL") + list(APPEND HALIDE_COMPILE_DEFINITIONS "-DWITH_METAL") endif() if (TARGET_D3D12COMPUTE) - target_compile_definitions(Halide PRIVATE "-DWITH_D3D12") + list(APPEND HALIDE_COMPILE_DEFINITIONS "-DWITH_D3D12") endif() -target_compile_definitions(Halide PRIVATE "-DLLVM_VERSION=${LLVM_VERSION}") -target_compile_definitions(Halide PRIVATE "-DCOMPILING_HALIDE") -target_compile_definitions(Halide PRIVATE ${LLVM_DEFINITIONS}) +list(APPEND HALIDE_COMPILE_DEFINITIONS "-DLLVM_VERSION=${LLVM_VERSION}") +list(APPEND HALIDE_COMPILE_DEFINITIONS "-DCOMPILING_HALIDE") + +# target_compile_definitions(${OBJ} PRIVATE ${HALIDE_COMPILE_DEFINITIONS} ${LLVM_DEFINITIONS}) if (NOT LLVM_ENABLE_ASSERTIONS) - target_compile_definitions(Halide PRIVATE NDEBUG) + list(APPEND HALIDE_COMPILE_DEFINITIONS NDEBUG) endif() +# The list of compiler options to pass when compiling +set(HALIDE_COMPILE_OPTIONS ) + if (MSVC) + list(APPEND HALIDE_COMPILE_DEFINITIONS "-D_CRT_SECURE_NO_WARNINGS") + list(APPEND HALIDE_COMPILE_DEFINITIONS "-D_SCL_SECURE_NO_WARNINGS") + # Suppress some warnings - # 4244: conversion, possible loss of data - # 4267: conversion, possible loss of data - # 4800: BOOL -> true or false - # 4996: compiler encountered deprecated declaration - target_compile_options(Halide PUBLIC /wd4244 /wd4267 /wd4800 /wd4996) - # Injected from recent LLVM: - target_compile_options(Halide PUBLIC /wd4141) # 'inline' used more than once - target_compile_options(Halide PUBLIC /wd4146) # unary minus applied to unsigned type - target_compile_options(Halide PUBLIC /wd4291) # No matching operator delete found - - target_compile_definitions(Halide PUBLIC "-D_CRT_SECURE_NO_WARNINGS" "-D_SCL_SECURE_NO_WARNINGS") + list(APPEND HALIDE_COMPILE_OPTIONS /wd4244) # 4244: conversion, possible loss of data + list(APPEND HALIDE_COMPILE_OPTIONS /wd4267) # 4267: conversion, possible loss of data + list(APPEND HALIDE_COMPILE_OPTIONS /wd4800) # 4800: BOOL -> true or false + list(APPEND HALIDE_COMPILE_OPTIONS /wd4996) # 4996: compiler encountered deprecated declaration + list(APPEND HALIDE_COMPILE_OPTIONS /wd4141) # 'inline' used more than once + list(APPEND HALIDE_COMPILE_OPTIONS /wd4146) # unary minus applied to unsigned type + list(APPEND HALIDE_COMPILE_OPTIONS /wd4291) # No matching operator delete found + # To compile LLVM headers following was taken from LLVM CMake files: # Disable sized deallocation if the flag is supported. MSVC fails to compile # the operator new overload in LLVM/IR/Function.h and Instruction.h otherwise. # See LLVM PR: 23513 (https://llvm.org/bugs/show_bug.cgi?id=23513) check_cxx_compiler_flag("/WX /Zc:sizedDealloc-" SUPPORTS_SIZED_DEALLOC) if (SUPPORTS_SIZED_DEALLOC) - target_compile_options(Halide PRIVATE "/Zc:sizedDealloc-") + target_compile_options(${OBJ} PRIVATE "/Zc:sizedDealloc-") endif() else() if (NOT HALIDE_ENABLE_RTTI) if (NOT MSVC) - target_compile_options(Halide PUBLIC "-fno-rtti") + list(APPEND HALIDE_COMPILE_OPTIONS "-fno-rtti") else() - target_compile_options(Halide PUBLIC "/GR-") + list(APPEND HALIDE_COMPILE_OPTIONS "/GR-") endif() endif() endif() -# Get the LLVM libraries we need -llvm_map_components_to_libnames(LIBS ${LLVM_COMPONENTS}) - -# When building a shared library the LLVM libraries will be -# embedded in the Halide library. When building a static library -# LLVM is not embedded but CMake knows that when building an executable -# against the Halide static library that it needs to link LLVM too so -# PRIVATE scope is the correct choice here. -target_link_libraries(Halide PRIVATE ${LIBS}) - -if (NOT MSVC) - set(LLVM_CONFIG ${LLVM_TOOLS_BINARY_DIR}/llvm-config) - execute_process(COMMAND "${LLVM_CONFIG}" --system-libs ${LLVM_COMPONENTS} OUTPUT_VARIABLE EXTRA_LIBS) - string(STRIP EXTRA_LIBS "${EXTRA_LIBS}") - string(REPLACE "-l" ";" EXTRA_LIBS "${EXTRA_LIBS}") - string(REPLACE "\n" "" EXTRA_LIBS "${EXTRA_LIBS}") - string(REPLACE " " "" EXTRA_LIBS "${EXTRA_LIBS}") - target_link_libraries(Halide PUBLIC ${EXTRA_LIBS}) -endif() +set(OBJECT_FILES ) + +foreach (MOD ${INITIAL_MODULES}) + set(OBJ "${MOD}_o") + add_library(${OBJ} OBJECT ${MOD}) + list(APPEND OBJECT_FILES "$") +endforeach() + +foreach (SRC ${SOURCE_FILES}) + set(OBJ "${SRC}_o") + add_library(${OBJ} OBJECT ${SRC} ${HEADER_FILES}) + + # Note when PUBLIC or INTERFACE scope is used in target_compile_* then targets + # that link against the Halide library inherit those options and definitions + target_include_directories(${OBJ} PRIVATE ${LLVM_INCLUDE_DIRS}) + target_include_directories(${OBJ} INTERFACE "${CMAKE_BINARY_DIR}/include") + + target_compile_definitions(${OBJ} PRIVATE ${HALIDE_COMPILE_DEFINITIONS} ${LLVM_DEFINITIONS}) + target_compile_options(${OBJ} PUBLIC ${HALIDE_COMPILE_OPTIONS}) + + list(APPEND OBJECT_FILES "$") +endforeach() + +function(add_halide_library HALIDE_LIBRARY_NAME HALIDE_LIBRARY_TYPE) + + add_library(${HALIDE_LIBRARY_NAME} ${HALIDE_LIBRARY_TYPE} ${HEADER_FILES} ${OBJECT_FILES}) + set_target_properties(${HALIDE_LIBRARY_NAME} PROPERTIES OUTPUT_NAME "Halide") + add_dependencies(${HALIDE_LIBRARY_NAME} HalideIncludes) + + # We could expose the /MP flag to all targets, but that might end up saturating the build + # since multiple MSBuild projects might get built in parallel, each of which compiling their + # source files in parallel; the Halide library itself is a "knot" point of the build graph, + # so compiling its files in parallel should not oversubscribe the system + target_compile_options(${HALIDE_LIBRARY_NAME} PUBLIC $<$:/MP>) + + # Get the LLVM libraries we need + llvm_map_components_to_libnames(LIBS ${LLVM_COMPONENTS}) + + # When building a shared library the LLVM libraries will be + # embedded in the Halide library. When building a static library + # LLVM is not embedded but CMake knows that when building an executable + # against the Halide static library that it needs to link LLVM too so + # PRIVATE scope is the correct choice here. + target_link_libraries(${HALIDE_LIBRARY_NAME} PRIVATE ${LIBS}) + + if (NOT MSVC) + set(LLVM_CONFIG ${LLVM_TOOLS_BINARY_DIR}/llvm-config) + execute_process(COMMAND "${LLVM_CONFIG}" --system-libs ${LLVM_COMPONENTS} OUTPUT_VARIABLE EXTRA_LIBS) + string(STRIP EXTRA_LIBS "${EXTRA_LIBS}") + string(REPLACE "-l" ";" EXTRA_LIBS "${EXTRA_LIBS}") + string(REPLACE "\n" "" EXTRA_LIBS "${EXTRA_LIBS}") + string(REPLACE " " "" EXTRA_LIBS "${EXTRA_LIBS}") + target_link_libraries(${HALIDE_LIBRARY_NAME} PUBLIC ${EXTRA_LIBS}) + endif() + + install(TARGETS ${HALIDE_LIBRARY_NAME} + RUNTIME DESTINATION bin + LIBRARY DESTINATION bin + ARCHIVE DESTINATION lib) +endfunction(add_halide_library) -install(TARGETS Halide - RUNTIME DESTINATION bin - LIBRARY DESTINATION bin - ARCHIVE DESTINATION lib) +add_halide_library(HalideShared SHARED) +add_halide_library(HalideStatic STATIC) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 272c262214cc..c3a23cfa8426 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -21,7 +21,7 @@ function(test_plain_c_includes) set(name "plain_c_includes") set(TEST_NAMES "${folder}_${name}") add_executable("${folder}_${name}" "${CMAKE_CURRENT_SOURCE_DIR}/${folder}/${name}.c") - target_include_directories("${folder}_${name}" PRIVATE "${CMAKE_BINARY_DIR}/include") + target_include_directories("${folder}_${name}" PRIVATE "${HALIDE_INCLUDE_DIR}") set(TEST_NAMES "${TEST_NAMES}" PARENT_SCOPE) endfunction(test_plain_c_includes) @@ -35,9 +35,14 @@ function(tests folder) list(APPEND TEST_NAMES "${TARGET}") halide_project("${TARGET}" "${folder}" "${folder}/${file}") - target_include_directories("${TARGET}" PRIVATE "${CMAKE_SOURCE_DIR}") target_compile_definitions("${TARGET}" PRIVATE "-DLLVM_VERSION=${LLVM_VERSION}") + target_include_directories("${TARGET}" PRIVATE "${HALIDE_INCLUDE_DIR}") + target_include_directories("${TARGET}" PRIVATE "${CMAKE_SOURCE_DIR}") + if("${folder}" STREQUAL "opengl") + target_include_directories("${TARGET}" PRIVATE "${CMAKE_SOURCE_DIR}/src") + endif() + set(GROUPS "test_${folder}") if("${folder}" STREQUAL "performance" OR "${folder}" STREQUAL "auto_schedule") # These shouldn't be part of run_tests, since they must be run with -j for timing purposes diff --git a/tools/build_halide_h.cpp b/tools/build_halide_h.cpp index f12bdc77ccde..c1387d34d6c8 100644 --- a/tools/build_halide_h.cpp +++ b/tools/build_halide_h.cpp @@ -75,12 +75,6 @@ int main(int argc, char **files) { fprintf(stdout, "#ifndef HALIDE_H\n"); fprintf(stdout, "#define HALIDE_H\n\n"); - // If we're building on visual studio and Halide_SHARED is defined, we'd better - // also define it for clients so that dllimport gets used. - #if defined(_MSC_VER) && defined(Halide_SHARED) - fprintf(stdout, "#define Halide_SHARED\n"); - #endif - for (int i = 2; i < argc; i++) { dump_header(files[i]); } diff --git a/tutorial/CMakeLists.txt b/tutorial/CMakeLists.txt index f34bcf7b911c..a2a46d14a49c 100644 --- a/tutorial/CMakeLists.txt +++ b/tutorial/CMakeLists.txt @@ -1,7 +1,5 @@ include(CMakeParseArguments) -include_directories("${CMAKE_BINARY_DIR}/tools") - # Detect if we need supress -Wunused-but-set-variable warning. This warning # might be emitted by gcc for the tutorials as they don't always use a result # when showing a demonstration of a computation in C code. @@ -24,7 +22,7 @@ function(add_tutorial source_file) add_halide_test("${name}" GROUPS test_tutorial WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}) - target_include_directories(${name} PRIVATE "${CMAKE_SOURCE_DIR}/tools") + target_include_directories(${name} PRIVATE "${HALIDE_TOOLS_DIR}") if (SUPPORT_NO_UNUSED_BUT_SET_VARIABLE) target_compile_options(${name} PRIVATE "-Wno-unused-but-set-variable") endif() @@ -92,7 +90,7 @@ if (BUILD_AOT_TUTORIAL) target_link_libraries(lesson_10_aot_compilation_run PRIVATE "${FILTER_DIR}/${FILTER_LIB}") target_include_directories(lesson_10_aot_compilation_run PRIVATE "${FILTER_DIR}") # Needed to find HalideBuffer.h - target_include_directories(lesson_10_aot_compilation_run PRIVATE "${CMAKE_BINARY_DIR}/include") + target_include_directories(lesson_10_aot_compilation_run PRIVATE "${HALIDE_INCLUDE_DIR}") if (NOT WIN32) target_link_libraries(lesson_10_aot_compilation_run PRIVATE dl pthread) diff --git a/util/CMakeLists.txt b/util/CMakeLists.txt index 81bb979d7ceb..a61abc11861f 100644 --- a/util/CMakeLists.txt +++ b/util/CMakeLists.txt @@ -1,3 +1,4 @@ halide_project(HalideTraceViz "utils" HalideTraceViz.cpp) + halide_project(HalideTraceDump "utils" HalideTraceDump.cpp HalideTraceUtils.cpp) halide_use_image_io(HalideTraceDump) From fbe62c1499b414d30f98bcfb32a7300a644c7c2a Mon Sep 17 00:00:00 2001 From: Steven Johnson Date: Mon, 8 Jul 2019 17:21:49 -0700 Subject: [PATCH 02/14] Update CMakeLists.txt --- test/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index c3a23cfa8426..f9b4ac14c6eb 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -10,6 +10,7 @@ option(WITH_TEST_AUTO_SCHEDULE "Build auto_schedule tests" ON) if (WITH_TEST_INTERNAL) message(STATUS "Internal tests enabled") halide_project(test_internal internal internal.cpp) + target_include_directories(test_internal PRIVATE "${CMAKE_SOURCE_DIR}/src") add_halide_test(test_internal GROUPS run_tests) else() From 436352c79321cc53f22e9dd69a47891f76a05d3f Mon Sep 17 00:00:00 2001 From: Steven Johnson Date: Mon, 8 Jul 2019 17:24:17 -0700 Subject: [PATCH 03/14] Update CMakeLists.txt --- src/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bfd32a93b149..7bc385efb5fd 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -735,12 +735,14 @@ set(OBJECT_FILES ) foreach (MOD ${INITIAL_MODULES}) set(OBJ "${MOD}_o") add_library(${OBJ} OBJECT ${MOD}) + set_property(TARGET ${OBJ} PROPERTY POSITION_INDEPENDENT_CODE ON) list(APPEND OBJECT_FILES "$") endforeach() foreach (SRC ${SOURCE_FILES}) set(OBJ "${SRC}_o") add_library(${OBJ} OBJECT ${SRC} ${HEADER_FILES}) + set_property(TARGET ${OBJ} PROPERTY POSITION_INDEPENDENT_CODE ON) # Note when PUBLIC or INTERFACE scope is used in target_compile_* then targets # that link against the Halide library inherit those options and definitions From 3d443e60327ad68f2eb067935d1e7eb83c732e0a Mon Sep 17 00:00:00 2001 From: Steven Johnson Date: Mon, 8 Jul 2019 17:32:57 -0700 Subject: [PATCH 04/14] Update CMakeLists.txt --- test/CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f9b4ac14c6eb..bf0746535729 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -11,8 +11,7 @@ if (WITH_TEST_INTERNAL) message(STATUS "Internal tests enabled") halide_project(test_internal internal internal.cpp) target_include_directories(test_internal PRIVATE "${CMAKE_SOURCE_DIR}/src") - add_halide_test(test_internal - GROUPS run_tests) + add_halide_test(test_internal GROUPS run_tests) else() message(WARNING "Internal tests disabled") endif() @@ -45,8 +44,9 @@ function(tests folder) endif() set(GROUPS "test_${folder}") - if("${folder}" STREQUAL "performance" OR "${folder}" STREQUAL "auto_schedule") + if("${folder}" STREQUAL "performance" OR "${folder}" STREQUAL "auto_schedule" OR "${folder}" STREQUAL "opengl") # These shouldn't be part of run_tests, since they must be run with -j for timing purposes + # Exception: opengl is excluded because Makefile excludes it too. Why? else() list(APPEND GROUPS run_tests) endif() From 4ec22f41c498cc440506e46a0183472e367508b2 Mon Sep 17 00:00:00 2001 From: Steven Johnson Date: Mon, 8 Jul 2019 17:56:37 -0700 Subject: [PATCH 05/14] Update CMakeLists.txt --- src/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7bc385efb5fd..5a44abca3ebb 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -718,7 +718,7 @@ if (MSVC) # See LLVM PR: 23513 (https://llvm.org/bugs/show_bug.cgi?id=23513) check_cxx_compiler_flag("/WX /Zc:sizedDealloc-" SUPPORTS_SIZED_DEALLOC) if (SUPPORTS_SIZED_DEALLOC) - target_compile_options(${OBJ} PRIVATE "/Zc:sizedDealloc-") + list(APPEND HALIDE_COMPILE_OPTIONS "/Zc:sizedDealloc-") endif() else() if (NOT HALIDE_ENABLE_RTTI) From 6b88943f4beebda62c90c3c03e6d1696bd3382e6 Mon Sep 17 00:00:00 2001 From: Steven Johnson Date: Mon, 8 Jul 2019 18:25:51 -0700 Subject: [PATCH 06/14] Update halide.cmake --- halide.cmake | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/halide.cmake b/halide.cmake index aae33d4b66cf..9f32e47d1241 100644 --- a/halide.cmake +++ b/halide.cmake @@ -77,13 +77,14 @@ function(halide_generator NAME) _halide_force_link_library("${NAME}_binary" "${GENLIB}") endif() - if ("${HALIDE_LIBRARY_TYPE}" STREQUAL "STATIC") + get_target_property(TARGET_TYPE "${HALIDE_COMPILER_LIB}" TYPE) + if("${TARGET_TYPE}" STREQUAL "STATIC_LIBRARY") # Getting link order correct for static libraries is nearly impossible in CMake; # to avoid flakiness, always link libHalide via --whole-archive # when in static-library mode. _halide_force_link_library("${NAME}_binary" "${HALIDE_COMPILER_LIB}") else() - target_link_libraries("${NAME}_binary" PRIVATE ${HALIDE_COMPILER_LIB}) + target_link_libraries("${NAME}_binary" PRIVATE "${HALIDE_COMPILER_LIB}") endif() _halide_genfiles_dir(${BASENAME} GENFILES_DIR) From d531c230eda90a9964f56df5a972d70561e27430 Mon Sep 17 00:00:00 2001 From: Steven Johnson Date: Mon, 8 Jul 2019 19:10:32 -0700 Subject: [PATCH 07/14] Update CMakeLists.txt --- src/CMakeLists.txt | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5a44abca3ebb..bc0dfe364142 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -172,36 +172,41 @@ function(add_runtime_modules TARGET_RUNTIME_FILES TARGET_RUNTIME_DIR) # -std=gnu++98 is deliberate; we do NOT want c++11 here, # as we don't want static locals to get thread synchronization stuff. add_custom_command(OUTPUT "${LL_D}" + # COMMENT "${SOURCE} -> ${LL_D}" DEPENDS "${SOURCE}" COMMAND ${CLANG} ${CXX_WARNING_FLAGS} ${RUNTIME_DEBUG_FLAG} -DDEBUG_RUNTIME -O3 -fno-ms-compatibility -ffreestanding -fno-blocks -fno-exceptions -fno-unwind-tables -std=gnu++98 -m${j} -target "${TARGET}" "-I${TARGET_RUNTIME_DIR}" -DCOMPILING_HALIDE_RUNTIME "-DLLVM_VERSION=${LLVM_VERSION}" -DBITS_${j} -emit-llvm -S "${SOURCE}" -o "${LL_D}" - COMMENT "${SOURCE} -> ${LL_D}" # Make sure that the output of this command also depends # on the header files that ${SOURCE} uses # FIXME: Only works for makefile generator IMPLICIT_DEPENDS CXX "${SOURCE}" ) add_custom_command(OUTPUT "${LL}" + # COMMENT "${SOURCE} -> ${LL}" DEPENDS "${SOURCE}" COMMAND ${CLANG} ${CXX_WARNING_FLAGS} -O3 -fno-ms-compatibility -ffreestanding -fno-blocks -fno-exceptions -fno-unwind-tables -std=gnu++98 -m${j} -target "${TARGET}" "-I${TARGET_RUNTIME_DIR}" -DCOMPILING_HALIDE_RUNTIME "-DLLVM_VERSION=${LLVM_VERSION}" -DBITS_${j} -emit-llvm -S "${SOURCE}" -o "${LL}" - COMMENT "${SOURCE} -> ${LL}") + ) add_custom_command(OUTPUT "${BC_D}" + # COMMENT "${LL_D} -> ${BC_D}" DEPENDS "${LL_D}" COMMAND "${LLVM_AS}" "${LL_D}" -o "${BC_D}" - COMMENT "${LL_D} -> ${BC_D}") + ) add_custom_command(OUTPUT "${BC}" + # COMMENT "${LL} -> ${BC}" DEPENDS "${LL}" COMMAND "${LLVM_AS}" "${LL}" -o "${BC}" - COMMENT "${LL} -> ${BC}") + ) add_custom_command(OUTPUT "${INITMOD_D}" + # COMMENT "${BC_D} -> ${INITMOD_D}" DEPENDS "${BC_D}" binary2cpp COMMAND binary2cpp "halide_internal_initmod_${i}_${j}_debug" < "${BC_D}" > "${INITMOD_D}" - COMMENT "${BC_D} -> ${INITMOD_D}") + ) add_custom_command(OUTPUT "${INITMOD}" + # COMMENT "${BC} -> ${INITMOD}" DEPENDS "${BC}" binary2cpp COMMAND binary2cpp "halide_internal_initmod_${i}_${j}" < "${BC}" > "${INITMOD}" - COMMENT "${BC} -> ${INITMOD}") + ) list(APPEND INITIAL_MODULES ${INITMOD}) list(APPEND INITIAL_MODULES ${INITMOD_D}) endforeach() From 6781e01320a1927fad5ad9e3930bcd7d7b26f3a3 Mon Sep 17 00:00:00 2001 From: Steven Johnson Date: Mon, 8 Jul 2019 19:12:43 -0700 Subject: [PATCH 08/14] Update CMakeLists.txt --- src/CMakeLists.txt | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bc0dfe364142..a3718c1fdc0a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -223,29 +223,32 @@ foreach (i ${RUNTIME_LL} ) set(BC "${PROJECT_BINARY_DIR}/${PROJECT_NAME}.build/${NATIVE_INT_DIR}initmod.${i}.bc") set(INITMOD "${INITMOD_PREFIX}${i}.cpp") add_custom_command(OUTPUT "${BC}" + # COMMENT "${LL} -> ${BC}" DEPENDS "${LL}" COMMAND "${LLVM_AS}" "${LL}" -o "${BC}" - COMMENT "${LL} -> ${BC}") + ) add_custom_command(OUTPUT "${INITMOD}" + # COMMENT "${BC} -> ${INITMOD}" DEPENDS "${BC}" binary2cpp COMMAND binary2cpp "halide_internal_initmod_${i}_ll" < "${BC}" > "${INITMOD}" - COMMENT "${BC} -> ${INITMOD}") + ) list(APPEND INITIAL_MODULES "${INITMOD}") endforeach() foreach (i ${RUNTIME_BC} ) set(INITMOD "${INITMOD_PREFIX}ptx_${i}.cpp") add_custom_command(OUTPUT "${INITMOD}" + # COMMENT "Building initial module ptx_${i}..." DEPENDS binary2cpp COMMAND binary2cpp "halide_internal_initmod_ptx_${i}_ll" < "${NATIVE_RUNTIME_DIR}nvidia_libdevice_bitcode/libdevice.${i}.10.bc" > "${INITMOD}" - COMMENT "Building initial module ptx_${i}..." VERBATIM) list(APPEND INITIAL_MODULES "${INITMOD}") endforeach() add_custom_command(OUTPUT "${INITMOD_PREFIX}inlined_c.cpp" + # COMMENT "buffer_t.cpp -> ${INITMOD_PREFIX}inlined_c.cpp" DEPENDS "${NATIVE_RUNTIME_DIR}buffer_t.cpp" binary2cpp COMMAND binary2cpp "halide_internal_initmod_inlined_c" < "${NATIVE_RUNTIME_DIR}buffer_t.cpp" > "${INITMOD_PREFIX}inlined_c.cpp" - COMMENT "buffer_t.cpp -> ${INITMOD_PREFIX}inlined_c.cpp") + ) list(APPEND INITIAL_MODULES "${INITMOD_PREFIX}inlined_c.cpp") set(RUNTIME_HEADER_FILES @@ -265,9 +268,10 @@ set(RUNTIME_HEADER_FILES foreach (i ${RUNTIME_HEADER_FILES}) string(REPLACE "." "_" SYM_NAME "${i}") add_custom_command(OUTPUT "${INITMOD_PREFIX}${SYM_NAME}.cpp" + # COMMENT "${i} -> ${INITMOD_PREFIX}${SYM_NAME}.cpp" DEPENDS "${NATIVE_RUNTIME_DIR}${i}" binary2cpp COMMAND binary2cpp "halide_internal_runtime_header_${SYM_NAME}" < "${NATIVE_RUNTIME_DIR}${i}" > "${INITMOD_PREFIX}${SYM_NAME}.cpp" - COMMENT "${i} -> ${INITMOD_PREFIX}${SYM_NAME}.cpp") + ) list(APPEND INITIAL_MODULES "${INITMOD_PREFIX}${SYM_NAME}.cpp") endforeach() From 07aa066fbf82690ca6fe0286f5a96440725ec3f5 Mon Sep 17 00:00:00 2001 From: Steven Johnson Date: Tue, 9 Jul 2019 11:16:11 -0700 Subject: [PATCH 09/14] Update build_travis.sh --- test/scripts/build_travis.sh | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/test/scripts/build_travis.sh b/test/scripts/build_travis.sh index d023a32c07b1..fbf497f245ff 100755 --- a/test/scripts/build_travis.sh +++ b/test/scripts/build_travis.sh @@ -31,28 +31,21 @@ if [ ${BUILD_SYSTEM} = 'CMAKE' ]; then -G "Unix Makefiles" \ ../ - # Build and run internal tests - make ${MAKEFLAGS} Halide - make ${MAKEFLAGS} test_internal - - # Build the docs and run the tests - make doc - make ${MAKEFLAGS} test_correctness - make ${MAKEFLAGS} test_generator - elif [ ${BUILD_SYSTEM} = 'MAKE' ]; then export LLVM_CONFIG=/usr/local/llvm/bin/llvm-config ${LLVM_CONFIG} --cxxflags --libdir --bindir - # Build and run internal tests - make ${MAKEFLAGS} - - # Build the docs and run the tests - make doc - make ${MAKEFLAGS} test_correctness - make ${MAKEFLAGS} test_generator - else echo "Unexpected BUILD_SYSTEM: \"${BUILD_SYSTEM}\"" exit 1 fi + +# Build and run internal tests +make ${MAKEFLAGS} distrib +make ${MAKEFLAGS} test_internal + +# Build the docs and run the tests +make doc +make ${MAKEFLAGS} test_correctness +make ${MAKEFLAGS} test_generator + From 075926e027cabeed18ba936cc63201e644d84619 Mon Sep 17 00:00:00 2001 From: Steven Johnson Date: Tue, 9 Jul 2019 13:29:33 -0700 Subject: [PATCH 10/14] Fixes --- CMakeLists.txt | 1 + test/CMakeLists.txt | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6ab329006de5..ba3f13d08be3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -370,6 +370,7 @@ define_test_group(test_auto_schedule) define_test_group(test_correctness) define_test_group(test_error) define_test_group(test_generator) +define_test_group(test_internal) define_test_group(test_opengl) define_test_group(test_performance) define_test_group(test_tutorial) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index bf0746535729..096674eae834 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -9,9 +9,9 @@ option(WITH_TEST_AUTO_SCHEDULE "Build auto_schedule tests" ON) if (WITH_TEST_INTERNAL) message(STATUS "Internal tests enabled") - halide_project(test_internal internal internal.cpp) - target_include_directories(test_internal PRIVATE "${CMAKE_SOURCE_DIR}/src") - add_halide_test(test_internal GROUPS run_tests) + halide_project(_test_internal internal internal.cpp) + target_include_directories(_test_internal PRIVATE "${CMAKE_SOURCE_DIR}/src") + add_halide_test(_test_internal GROUPS test_internal run_tests) else() message(WARNING "Internal tests disabled") endif() From dfe5c18ceac8669cf8066be5a123af8d2be2c14b Mon Sep 17 00:00:00 2001 From: Steven Johnson Date: Tue, 9 Jul 2019 14:48:33 -0700 Subject: [PATCH 11/14] Fixes --- .travis.yml | 4 ++-- test/scripts/build_travis.sh | 30 +++++++++++++++++++++--------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index 96aab79b347a..842cd67983d6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,8 +20,8 @@ env: # # Note that gcc5.4 is the default install on Travis Xenial, so we'll just use that. - LLVM_VERSION=8.0.0 BUILD_SYSTEM=MAKE - - LLVM_VERSION=8.0.0 BUILD_SYSTEM=CMAKE HALIDE_SHARED_LIBRARY=1 - - LLVM_VERSION=8.0.0 BUILD_SYSTEM=CMAKE HALIDE_SHARED_LIBRARY=0 + - LLVM_VERSION=8.0.0 BUILD_SYSTEM=CMAKE HALIDE_SHARED_LIBRARY=ON + - LLVM_VERSION=8.0.0 BUILD_SYSTEM=CMAKE HALIDE_SHARED_LIBRARY=OFF # # llvm7 prebuilts are cranky on Travis and give flaky failures; we just skipped them # in Trusty and continue to skip them in Xenial. diff --git a/test/scripts/build_travis.sh b/test/scripts/build_travis.sh index fbf497f245ff..6eed01c6462c 100755 --- a/test/scripts/build_travis.sh +++ b/test/scripts/build_travis.sh @@ -31,21 +31,33 @@ if [ ${BUILD_SYSTEM} = 'CMAKE' ]; then -G "Unix Makefiles" \ ../ + make ${MAKEFLAGS} distrib + make ${MAKEFLAGS} test_internal + + if [ ${HALIDE_SHARED_LIBRARY} = 'ON' ]; then + # Building with static library is slower, and can run + # over the time limit; since we just want a reality + # check, do the full test suite only for shared. + make ${MAKEFLAGS} test_correctness + make ${MAKEFLAGS} test_generator + make doc + endif() + elif [ ${BUILD_SYSTEM} = 'MAKE' ]; then export LLVM_CONFIG=/usr/local/llvm/bin/llvm-config ${LLVM_CONFIG} --cxxflags --libdir --bindir + # Build and run internal tests + make ${MAKEFLAGS} distrib + make ${MAKEFLAGS} test_internal + + # Build the docs and run the tests + make doc + make ${MAKEFLAGS} test_correctness + make ${MAKEFLAGS} test_generator + else echo "Unexpected BUILD_SYSTEM: \"${BUILD_SYSTEM}\"" exit 1 fi -# Build and run internal tests -make ${MAKEFLAGS} distrib -make ${MAKEFLAGS} test_internal - -# Build the docs and run the tests -make doc -make ${MAKEFLAGS} test_correctness -make ${MAKEFLAGS} test_generator - From 2c69f67006dc5561a3d67e49002ed8b02a5f54a4 Mon Sep 17 00:00:00 2001 From: Steven Johnson Date: Tue, 9 Jul 2019 14:53:22 -0700 Subject: [PATCH 12/14] Update build_travis.sh --- test/scripts/build_travis.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/scripts/build_travis.sh b/test/scripts/build_travis.sh index 6eed01c6462c..819d0bb60f96 100755 --- a/test/scripts/build_travis.sh +++ b/test/scripts/build_travis.sh @@ -41,7 +41,7 @@ if [ ${BUILD_SYSTEM} = 'CMAKE' ]; then make ${MAKEFLAGS} test_correctness make ${MAKEFLAGS} test_generator make doc - endif() + fi elif [ ${BUILD_SYSTEM} = 'MAKE' ]; then export LLVM_CONFIG=/usr/local/llvm/bin/llvm-config From 8132e3bbb790276268a42cf0d25b53a10cb6a736 Mon Sep 17 00:00:00 2001 From: Steven Johnson Date: Tue, 9 Jul 2019 16:04:38 -0700 Subject: [PATCH 13/14] Fixes --- src/CMakeLists.txt | 37 ++++++++++++------------------------- src/Util.h | 1 + 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a3718c1fdc0a..49d280b128c7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -739,36 +739,23 @@ else() endif() endif() -set(OBJECT_FILES ) +function(add_halide_library HALIDE_LIBRARY_NAME HALIDE_LIBRARY_TYPE) -foreach (MOD ${INITIAL_MODULES}) - set(OBJ "${MOD}_o") - add_library(${OBJ} OBJECT ${MOD}) - set_property(TARGET ${OBJ} PROPERTY POSITION_INDEPENDENT_CODE ON) - list(APPEND OBJECT_FILES "$") -endforeach() + add_library(${HALIDE_LIBRARY_NAME} ${HALIDE_LIBRARY_TYPE} + ${HEADER_FILES} + ${SOURCE_FILES} + ${INITIAL_MODULES} + ) + set_target_properties(${HALIDE_LIBRARY_NAME} PROPERTIES OUTPUT_NAME "Halide") + add_dependencies(${HALIDE_LIBRARY_NAME} HalideIncludes) -foreach (SRC ${SOURCE_FILES}) - set(OBJ "${SRC}_o") - add_library(${OBJ} OBJECT ${SRC} ${HEADER_FILES}) - set_property(TARGET ${OBJ} PROPERTY POSITION_INDEPENDENT_CODE ON) + target_compile_definitions(${HALIDE_LIBRARY_NAME} PRIVATE ${HALIDE_COMPILE_DEFINITIONS} ${LLVM_DEFINITIONS}) + target_compile_options(${HALIDE_LIBRARY_NAME} PRIVATE ${HALIDE_COMPILE_OPTIONS}) # Note when PUBLIC or INTERFACE scope is used in target_compile_* then targets # that link against the Halide library inherit those options and definitions - target_include_directories(${OBJ} PRIVATE ${LLVM_INCLUDE_DIRS}) - target_include_directories(${OBJ} INTERFACE "${CMAKE_BINARY_DIR}/include") - - target_compile_definitions(${OBJ} PRIVATE ${HALIDE_COMPILE_DEFINITIONS} ${LLVM_DEFINITIONS}) - target_compile_options(${OBJ} PUBLIC ${HALIDE_COMPILE_OPTIONS}) - - list(APPEND OBJECT_FILES "$") -endforeach() - -function(add_halide_library HALIDE_LIBRARY_NAME HALIDE_LIBRARY_TYPE) - - add_library(${HALIDE_LIBRARY_NAME} ${HALIDE_LIBRARY_TYPE} ${HEADER_FILES} ${OBJECT_FILES}) - set_target_properties(${HALIDE_LIBRARY_NAME} PROPERTIES OUTPUT_NAME "Halide") - add_dependencies(${HALIDE_LIBRARY_NAME} HalideIncludes) + target_include_directories(${HALIDE_LIBRARY_NAME} PRIVATE ${LLVM_INCLUDE_DIRS}) + target_include_directories(${HALIDE_LIBRARY_NAME} INTERFACE "${CMAKE_BINARY_DIR}/include") # We could expose the /MP flag to all targets, but that might end up saturating the build # since multiple MSBuild projects might get built in parallel, each of which compiling their diff --git a/src/Util.h b/src/Util.h index 77b8c1cf4c53..bfd20c9cff74 100644 --- a/src/Util.h +++ b/src/Util.h @@ -24,6 +24,7 @@ #ifndef HALIDE_EXPORT #if defined(_MSC_VER) +// Halide_EXPORTS is quietly defined by CMake when building a shared library #ifdef Halide_EXPORTS #define HALIDE_EXPORT __declspec(dllexport) #else From 3df808c4e8ab47ece975b04872e28a41e96a0d7c Mon Sep 17 00:00:00 2001 From: Steven Johnson Date: Tue, 9 Jul 2019 16:57:22 -0700 Subject: [PATCH 14/14] Windows --- src/CMakeLists.txt | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 49d280b128c7..ae8824297b0e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -746,7 +746,19 @@ function(add_halide_library HALIDE_LIBRARY_NAME HALIDE_LIBRARY_TYPE) ${SOURCE_FILES} ${INITIAL_MODULES} ) - set_target_properties(${HALIDE_LIBRARY_NAME} PROPERTIES OUTPUT_NAME "Halide") + + if (NOT MSVC) + # HalideShared -> libHalide.so (or libHalide.dylib) + # HalideStatic -> libHalide.a + set_target_properties(${HALIDE_LIBRARY_NAME} PROPERTIES OUTPUT_NAME "Halide") + else() + # HalideShared -> Halide.dll and Halide.lib + # HalideStatic -> HalideStatic.lib + if("${HALIDE_LIBRARY_TYPE}" STREQUAL "SHARED") + set_target_properties(${HALIDE_LIBRARY_NAME} PROPERTIES OUTPUT_NAME "Halide") + endif() + endif() + add_dependencies(${HALIDE_LIBRARY_NAME} HalideIncludes) target_compile_definitions(${HALIDE_LIBRARY_NAME} PRIVATE ${HALIDE_COMPILE_DEFINITIONS} ${LLVM_DEFINITIONS})