Skip to content

Commit

Permalink
Merge pull request #2738 from kernhanda/kernhanda/cmake-package-pr
Browse files Browse the repository at this point in the history
Rewrite CMake builds `install`/`distrib` targets.
  • Loading branch information
abadams authored Feb 14, 2018
2 parents d5621f5 + 9bec1f6 commit b848f79
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 185 deletions.
205 changes: 72 additions & 133 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@
project(Halide)
cmake_minimum_required(VERSION 3.2)

set(CPACK_PACKAGE_VENDOR "Halide")
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/LICENSE.txt")
set(CPACK_MONOLITHIC_INSTALL OFF)
if (WIN32)
set(CPACK_GENERATOR "ZIP")
else()
set(CPACK_GENERATOR "TGZ")
endif()
# Remove this to get package names that are formatted as
# ${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_SYSTEM_NAME}.
set(CPACK_PACKAGE_FILE_NAME "Halide" CACHE STRING "Name of package created by distrib target")
include(CPack)

find_package(Threads QUIET)

if("${HALIDE_REQUIRE_LLVM_VERSION}" STREQUAL "")
Expand All @@ -22,7 +35,7 @@ endif()
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")

if (MSVC)
if (MSVC AND NOT CMAKE_CONFIGURATION_TYPES)
# LLVM5.x on Windows can include "$(Configuration) in the path";
# fix this so we can use the paths right away.
string(REPLACE "$(Configuration)" ${CMAKE_BUILD_TYPE} LLVM_TOOLS_BINARY_DIR "${LLVM_TOOLS_BINARY_DIR}")
Expand Down Expand Up @@ -56,7 +69,7 @@ file(TO_NATIVE_PATH "${LLVM_TOOLS_BINARY_DIR}/llvm-nm${CMAKE_EXECUTABLE_SUFFIX}"
file(TO_NATIVE_PATH "${LLVM_TOOLS_BINARY_DIR}/clang${CMAKE_EXECUTABLE_SUFFIX}" CLANG)
file(TO_NATIVE_PATH "${LLVM_TOOLS_BINARY_DIR}/llvm-config${CMAKE_EXECUTABLE_SUFFIX}" LLVM_CONFIG)

# LLVM doesn't appear to expose --system-libs via its CMake interface,
# LLVM doesn't appear to expose --system-libs via its CMake interface,
# so we must shell out to llvm-config to find this info
execute_process(COMMAND ${LLVM_CONFIG} --system-libs --link-static OUTPUT_VARIABLE HALIDE_SYSTEM_LIBS_RAW)
string(STRIP "${HALIDE_SYSTEM_LIBS_RAW}" HALIDE_SYSTEM_LIBS_RAW) # strip whitespace from start & end
Expand Down Expand Up @@ -155,6 +168,9 @@ endif()
function(halide_project name folder)
add_executable("${name}" ${ARGN})
if (MSVC)
if(NOT HALIDE_ENABLE_RTTI)
target_compile_options("${name}" PUBLIC "/GR-")
endif()
else()
if (NOT HALIDE_ENABLE_RTTI)
target_compile_options("${name}" PUBLIC "-fno-rtti")
Expand Down Expand Up @@ -243,7 +259,7 @@ endforeach()

function(define_test_group GROUP)
if(TARGET "${GROUP}")
message(FATAL_ERROR "Group ${GROUP} is already defined.")
message(FATAL_ERROR "Group ${GROUP} is already defined.")
endif()
add_custom_target("${GROUP}")
set_target_properties("${GROUP}" PROPERTIES EXCLUDE_FROM_ALL TRUE)
Expand Down Expand Up @@ -292,7 +308,7 @@ function(add_test TARGET)
else()
set(COMMAND "${TARGET}")
endif()
add_custom_target("${EXEC_NAME}"
add_custom_target("${EXEC_NAME}"
COMMAND ${COMMAND}
WORKING_DIRECTORY ${args_WORKING_DIRECTORY}
COMMENT "Executing ${TARGET}"
Expand Down Expand Up @@ -333,15 +349,12 @@ endif()

option(WITH_APPS "Build apps" ON)
if (WITH_APPS)
if (NOT WIN32)
message(STATUS "Building apps enabled")
add_subdirectory(apps)
else()
message(WARNING "Apps cannot be built under Windows using cmake. Try the makefile in an msys2 shell.")
endif()
message(STATUS "Building apps enabled")
add_subdirectory(apps)
else()
message(STATUS "Building apps disabled")
endif()

option(WITH_TUTORIALS "Build Tutorials" ON)
if (WITH_TUTORIALS)
message(STATUS "Building tutorials enabled")
Expand Down Expand Up @@ -375,149 +388,75 @@ else()
endif()

# ------------------------------------------------
# 'make distrib' support

add_custom_target(build_distrib_dir)
set(DISTRIB_DIR "${CMAKE_BINARY_DIR}/distrib")

function(_make_distrib_subdir TARGET DIR)
set(NAME "_make_distrib_dir_${TARGET}")
add_custom_target("${NAME}"
COMMAND cmake -E make_directory "${DISTRIB_DIR}/${DIR}")
set_target_properties("${NAME}" PROPERTIES EXCLUDE_FROM_ALL TRUE)
add_dependencies("${TARGET}" "${NAME}" )
endfunction()

# Copy the file produced by TARGET into the distrib folder, in the subdir DIR
function(add_to_distrib TARGET DIR)
set(options )
set(oneValueArgs )
set(multiValueArgs DEPS)
cmake_parse_arguments(args "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(TARGET "${TARGET}")
set(NAME "_distrib_${TARGET}")
get_target_property(TARGET_TYPE "${TARGET}" TYPE)
if("${TARGET_TYPE}" STREQUAL "EXECUTABLE" OR
"${TARGET_TYPE}" STREQUAL "STATIC_LIBRARY" OR
"${TARGET_TYPE}" STREQUAL "SHARED_LIBRARY")
add_custom_target("${NAME}"
COMMAND cmake -E copy $<TARGET_FILE:${TARGET}> "${DISTRIB_DIR}/${DIR}"
DEPENDS "${TARGET}")
_make_distrib_subdir("${NAME}" "${DIR}")
else()
message(FATAL_ERROR "TARGET ${TARGET} has type ${TARGET_TYPE}")
endif()
set_target_properties("${NAME}" PROPERTIES EXCLUDE_FROM_ALL TRUE)
else()
get_filename_component(FNAME "${TARGET}" NAME)
set(NAME "_distrib_${DIR}_${FNAME}")
string(REPLACE "/" "_" NAME "${NAME}")
string(REPLACE "\\" "_" NAME "${NAME}")
add_custom_target("${NAME}"
COMMAND cmake -E copy "${TARGET}" "${DISTRIB_DIR}/${DIR}"
DEPENDS "${TARGET}")
_make_distrib_subdir("${NAME}" "${DIR}")
endif()
if(NOT "${args_DEPS}" STREQUAL "")
add_dependencies("${NAME}" ${args_DEPS})
endif()
add_dependencies(build_distrib_dir "${NAME}")
endfunction()
# install

# ---- libHalide
if("${HALIDE_LIBRARY_TYPE}" STREQUAL "SHARED")
add_to_distrib(Halide bin)
else()
add_to_distrib(Halide lib)
endif()

# ---- Header files
add_to_distrib("${CMAKE_BINARY_DIR}/include/Halide.h" include
DEPS HalideIncludes)
add_to_distrib("${CMAKE_BINARY_DIR}/include/HalideBuffer.h" include)
file(GLOB FILES "${CMAKE_BINARY_DIR}/include/HalideRuntime*.h")
foreach(F ${FILES})
add_to_distrib(${F} include)
endforeach()

# ---- Tutorials
foreach(EXT cpp h sh)
set(DIR "${CMAKE_SOURCE_DIR}/tutorial")
file(GLOB FILES "${DIR}/*.${EXT}")
foreach(F ${FILES})
add_to_distrib(${F} tutorial)
endforeach()
endforeach()
foreach(EXT gif jpg mp4)
set(DIR "${CMAKE_SOURCE_DIR}/tutorial/figures")
file(GLOB FILES "${DIR}/*.${EXT}")
foreach(F ${FILES})
add_to_distrib(${F} tutorial/figures)
endforeach()
endforeach()
foreach(EXT png)
set(DIR "${CMAKE_SOURCE_DIR}/tutorial/images")
file(GLOB FILES "${DIR}/*.${EXT}")
foreach(F ${FILES})
add_to_distrib(${F} tutorial/images)
endforeach()
endforeach()
install(FILES
"${CMAKE_BINARY_DIR}/include/Halide.h"
"${CMAKE_BINARY_DIR}/include/HalideBuffer.h"
${FILES}
DESTINATION include
)

install(DIRECTORY tutorial
DESTINATION tutorial
FILES_MATCHING
PATTERN "*.h"
PATTERN "*.sh"
PATTERN "*.gif"
PATTERN "*.jpg"
PATTERN "*.mp4"
PATTERN "*.png")

# ---- Tools
foreach(F mex_halide.m
GenGen.cpp
RunGen.cpp
RunGenStubs.cpp
halide_benchmark.h
halide_image.h
halide_image_io.h
foreach(F mex_halide.m
GenGen.cpp
RunGen.cpp
RunGenStubs.cpp
halide_benchmark.h
halide_image.h
halide_image_io.h
halide_image_info.h)
add_to_distrib("${CMAKE_SOURCE_DIR}/tools/${F}" tools)
install(FILES "${CMAKE_SOURCE_DIR}/tools/${F}"
DESTINATION tools)
endforeach()

# ---- README
file(GLOB FILES "${CMAKE_SOURCE_DIR}/*.md")
foreach(F ${FILES})
add_to_distrib(${F} "")
endforeach()
install(FILES ${FILES}
DESTINATION .)

# ---- Bazel
# ---- Bazel
file(GLOB FILES "${CMAKE_SOURCE_DIR}/bazel/*")
foreach(F ${FILES})
add_to_distrib(${F} "")
endforeach()
install(FILES ${FILES}
DESTINATION .)

# ---- CMake
# ---- CMake
file(GLOB FILES "${CMAKE_SOURCE_DIR}/*.cmake")
foreach(F ${FILES})
add_to_distrib(${F} "")
endforeach()
install(FILES ${FILES}
DESTINATION .)

# ---- halide_config
# ---- halide_config
file(GLOB FILES "${CMAKE_SOURCE_DIR}/tools/halide_config.*.tpl")
foreach(F ${FILES})
# Can't rely on sed on Windows, so just do the trivial transform we need right here.
file(READ ${F} CONTENTS)
get_filename_component(FNAME "${F}" NAME) # Extract filename
string(REGEX REPLACE "\\.tpl$" "" FNAME "${FNAME}") # Strip .tpl extension
string(REPLACE "\${LLVM_SYSTEM_LIBS}" "${HALIDE_SYSTEM_LIBS_RAW}" NEW_CONTENTS "${CONTENTS}")
file(WRITE "${CMAKE_BINARY_DIR}/${FNAME}" ${NEW_CONTENTS})
add_to_distrib("${CMAKE_BINARY_DIR}/${FNAME}" "")
configure_file("${F}" "${CMAKE_BINARY_DIR}/${FNAME}" @ONLY)
install(FILES "${CMAKE_BINARY_DIR}/${FNAME}"
DESTINATION .)
endforeach()

# ---- distrib.tgz and distrib.zip
file(TO_NATIVE_PATH "${DISTRIB_DIR}" NATIVE_DISTRIB_DIR)
file(TO_NATIVE_PATH "${DISTRIB_DIR}/../" NATIVE_ARCHIVE_DIR)
if (MSVC)
# 'cmake -E tar' can create zipfiles as of v3.2+
add_custom_target(halide_archive
DEPENDS build_distrib_dir
COMMAND cmake -E tar -cf "${NATIVE_ARCHIVE_DIR}/halide.zip" --format=zip "${NATIVE_DISTRIB_DIR}")
if (NOT WIN32)
set(CPACK_PACKAGE_EXT "tar.gz")
set(DISTRIB_PACKAGE_EXT "tgz")
else()
add_custom_target(halide_archive
DEPENDS build_distrib_dir
COMMAND cmake -E tar -czf "${NATIVE_ARCHIVE_DIR}/halide.tgz" "${NATIVE_DISTRIB_DIR}")
set(CPACK_PACKAGE_EXT "zip")
set(DISTRIB_PACKAGE_EXT ${CPACK_PACKAGE_EXT})
endif()

add_custom_target(distrib)
add_dependencies(distrib build_distrib_dir halide_archive)
add_custom_target(distrib
COMMAND cmake --build . --config $<CONFIG> --target package
COMMAND cmake -E echo "${CPACK_PACKAGE_VENDOR}.${CPACK_PACKAGE_EXT} \"=>\" ${CPACK_PACKAGE_VENDOR}.${DISTRIB_PACKAGE_EXT}"
COMMAND cmake -E rename "${CPACK_PACKAGE_VENDOR}.${CPACK_PACKAGE_EXT}" "${CPACK_PACKAGE_VENDOR}.${DISTRIB_PACKAGE_EXT}"
BYPRODUCTS "${CPACK_PACKAGE_VENDOR}.${DISTRIB_PACKAGE_EXT}"
USES_TERMINAL)
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,7 @@ $(BIN_DIR)/correctness_plain_c_includes: $(ROOT_DIR)/test/correctness/plain_c_in
$(CXX) -x c -Wall -Werror -I$(ROOT_DIR) $(OPTIMIZE_FOR_BUILD_TIME) $< -I$(ROOT_DIR)/src/runtime -o $@

# Note that this test must *not* link in either libHalide, or a Halide runtime;
# this test should be usable without either.
# this test should be usable without either.
$(BIN_DIR)/correctness_halide_buffer: $(ROOT_DIR)/test/correctness/halide_buffer.cpp $(INCLUDE_DIR)/HalideBuffer.h $(RUNTIME_EXPORTED_INCLUDES)
$(CXX) $(TEST_CXX_FLAGS) $(OPTIMIZE_FOR_BUILD_TIME) $< -I$(INCLUDE_DIR) -o $@

Expand Down Expand Up @@ -1414,8 +1414,8 @@ $(BIN_DIR)/tutorial_lesson_21_auto_scheduler_generate: $(ROOT_DIR)/tutorial/less
-I$(INCLUDE_DIR) $(TEST_LD_FLAGS) $(IMAGE_IO_LIBS) -o $@

# The values in MachineParams are:
# - the maximum level of parallelism available,
# - the size of the last-level cache (in KB),
# - the maximum level of parallelism available,
# - the size of the last-level cache (in KB),
# - the ratio between the cost of a miss at the last level cache and the cost
# of arithmetic on the target architecture
# ...in that order.
Expand Down Expand Up @@ -1718,7 +1718,7 @@ install_qc: install $(HEXAGON_RUNTIME_LIBS)
# we include in our distribution.
$(BUILD_DIR)/halide_config.%: $(ROOT_DIR)/tools/halide_config.%.tpl
@mkdir -p $(@D)
cat $< | sed -e 's/$${LLVM_SYSTEM_LIBS}/${LLVM_SYSTEM_LIBS}/g' > $@
cat $< | sed -e 's/@HALIDE_SYSTEM_LIBS_RAW@/${LLVM_SYSTEM_LIBS}/g' > $@

$(DISTRIB_DIR)/halide.tgz: $(LIB_DIR)/libHalide.a \
$(BIN_DIR)/libHalide.$(SHARED_EXT) \
Expand Down
6 changes: 6 additions & 0 deletions apps/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
include_directories("${CMAKE_BINARY_DIR}/include")
include_directories("${CMAKE_SOURCE_DIR}/tools")

if (MSVC)
# Sample apps, so we can disable some of the more pedantic warnings
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
add_compile_options(/wd4244) # conversion from 'type1' to 'type2', possible loss of data.
endif()

add_subdirectory(bilateral_grid)
add_subdirectory(blur)
add_subdirectory(c_backend)
Expand Down
6 changes: 6 additions & 0 deletions apps/camera_pipe/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
if(WIN32)
# Building this app for x86 in VS seems to have issues
# https://github.com/halide/Halide/pull/2740
return()
endif()

add_executable(camera_pipe_process process.cpp)
halide_use_image_io(camera_pipe_process)

Expand Down
4 changes: 4 additions & 0 deletions apps/glsl/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Generators
if (WIN32)
# TODO: Provide a version of halide_opengl_create_context for Windows
return()
endif()
find_package(OpenGL)
if (OpenGL_FOUND)

Expand Down
1 change: 1 addition & 0 deletions apps/linear_algebra/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ target_link_libraries(test_halide_blas
cblas # XXX fragile
Halide
)

8 changes: 7 additions & 1 deletion apps/resize/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
list(APPEND VARIANTS
if(WIN32)
# Building this app for x86 in VS seems to have issues
# https://github.com/halide/Halide/pull/2740
return()
endif()

list(APPEND VARIANTS
box_float32_up
box_float32_down
box_uint16_up
Expand Down
Loading

0 comments on commit b848f79

Please sign in to comment.