From f04f78542efba6d325d74fe9aab32eb5d3262054 Mon Sep 17 00:00:00 2001 From: Steven Johnson Date: Thu, 2 Nov 2017 17:56:15 -0700 Subject: [PATCH 1/6] WIP for cmake distrib - Needs to build static-library as well as shared - .tgz and .zip are outside distrib/ folder --- CMakeLists.txt | 136 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 132 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a51ef9ea6f84..d26c4d1c3845 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ project(Halide) -cmake_minimum_required(VERSION 3.1.3) +cmake_minimum_required(VERSION 3.2) find_package(Threads QUIET) @@ -38,9 +38,9 @@ file(TO_NATIVE_PATH "${LLVM_TOOLS_BINARY_DIR}/llvm-config${CMAKE_EXECUTABLE_SUFF # 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 OUTPUT_VARIABLE HALIDE_SYSTEM_LIBS) -string(STRIP "${HALIDE_SYSTEM_LIBS}" HALIDE_SYSTEM_LIBS) # strip whitespace from start & end -string(REPLACE " " ";" HALIDE_SYSTEM_LIBS "${HALIDE_SYSTEM_LIBS}") # convert into a list +execute_process(COMMAND ${LLVM_CONFIG} --system-libs OUTPUT_VARIABLE HALIDE_SYSTEM_LIBS_RAW) +string(STRIP "${HALIDE_SYSTEM_LIBS_RAW}" HALIDE_SYSTEM_LIBS_RAW) # strip whitespace from start & end +string(REPLACE " " ";" HALIDE_SYSTEM_LIBS "${HALIDE_SYSTEM_LIBS_RAW}") # convert into a list if("${HALIDE_SYSTEM_LIBS}" STREQUAL "") # It's theoretically possible that this could be legitimately empty, # but in practice that doesn't really happen, so we'll assume it means we @@ -304,6 +304,7 @@ endif() # add_to_group(run_tests test_performance BUILD) # add_to_group(run_tests test_auto_schedule BUILD) +# ------------------------------------------------ add_subdirectory(src) option(WITH_TESTS "Build tests" ON) @@ -356,3 +357,130 @@ if (WITH_UTILS) else() message(STATUS "Building utils disabled") endif() + +# ------------------------------------------------ + +add_custom_target(distrib) +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) + 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 $ "${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() + add_dependencies(distrib "${NAME}") +endfunction() + +# ---- 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) +add_to_distrib("${CMAKE_BINARY_DIR}/include/HalideBuffer.h" include) +file(GLOB FILES "${CMAKE_SOURCE_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() + +# ---- Tools +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) +endforeach() + +# ---- README +file(GLOB FILES "${CMAKE_SOURCE_DIR}/*.md") +foreach(F ${FILES}) + add_to_distrib(${F} "") +endforeach() + +# ---- Bazel +file(GLOB FILES "${CMAKE_SOURCE_DIR}/bazel/*") +foreach(F ${FILES}) + add_to_distrib(${F} "") +endforeach() + +# ---- 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}" "") +endforeach() + +# ---- distrib.tgz and distrib.zip +add_custom_target(halide.tgz + DEPENDS distrib + COMMAND cmake -E tar -czf "${CMAKE_BINARY_DIR}/halide.tgz" "${DISTRIB_DIR}") + +# 'cmake -E tar' can create zipfiles as of v3.2+ +add_custom_target(halide.zip + DEPENDS distrib + COMMAND cmake -E tar -cf "${CMAKE_BINARY_DIR}/halide.zip" --format=zip "${DISTRIB_DIR}") + From 0c1805c2a660553be983ad102a941644bb1530e5 Mon Sep 17 00:00:00 2001 From: Steven Johnson Date: Mon, 13 Nov 2017 11:19:56 -0800 Subject: [PATCH 2/6] Fixes --- CMakeLists.txt | 32 +++++++++++++++++++++---------- src/CMakeLists.txt | 48 +++++++++++++++++++++++++--------------------- 2 files changed, 48 insertions(+), 32 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 48653d622e95..e1a51f502432 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -358,8 +358,9 @@ else() endif() # ------------------------------------------------ +# 'make distrib' support -add_custom_target(distrib) +add_custom_target(build_distrib_dir) set(DISTRIB_DIR "${CMAKE_BINARY_DIR}/distrib") function(_make_distrib_subdir TARGET DIR) @@ -396,7 +397,7 @@ function(add_to_distrib TARGET DIR) DEPENDS "${TARGET}") _make_distrib_subdir("${NAME}" "${DIR}") endif() - add_dependencies(distrib "${NAME}") + add_dependencies(build_distrib_dir "${NAME}") endfunction() # ---- libHalide @@ -407,6 +408,7 @@ else() endif() # ---- Header files +add_dependencies(build_distrib_dir HalideIncludes) add_to_distrib("${CMAKE_BINARY_DIR}/include/Halide.h" include) add_to_distrib("${CMAKE_BINARY_DIR}/include/HalideBuffer.h" include) file(GLOB FILES "${CMAKE_SOURCE_DIR}/include/HalideRuntime*.h") @@ -461,6 +463,12 @@ foreach(F ${FILES}) add_to_distrib(${F} "") endforeach() +# ---- CMake +file(GLOB FILES "${CMAKE_SOURCE_DIR}/*.cmake") +foreach(F ${FILES}) + add_to_distrib(${F} "") +endforeach() + # ---- halide_config file(GLOB FILES "${CMAKE_SOURCE_DIR}/tools/halide_config.*.tpl") foreach(F ${FILES}) @@ -474,12 +482,16 @@ foreach(F ${FILES}) endforeach() # ---- distrib.tgz and distrib.zip -add_custom_target(halide.tgz - DEPENDS distrib - COMMAND cmake -E tar -czf "${CMAKE_BINARY_DIR}/halide.tgz" "${DISTRIB_DIR}") - -# 'cmake -E tar' can create zipfiles as of v3.2+ -add_custom_target(halide.zip - DEPENDS distrib - COMMAND cmake -E tar -cf "${CMAKE_BINARY_DIR}/halide.zip" --format=zip "${DISTRIB_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 "${DISTRIB_DIR}/halide.zip" --format=zip "${DISTRIB_DIR}") +else() + add_custom_target(halide_archive + DEPENDS build_distrib_dir + COMMAND cmake -E tar -czf "${DISTRIB_DIR}/halide.tgz" "${DISTRIB_DIR}") +endif() +add_custom_target(distrib) +add_dependencies(distrib build_distrib_dir halide_archive) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2111e4ce5fcc..8f514b828d7f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -239,9 +239,12 @@ foreach (i ${RUNTIME_HEADER_FILES}) list(APPEND INITIAL_MODULES "${INITMOD_PREFIX}${SYM_NAME}.cpp") endforeach() +# The externally-visible header files that go into making Halide.h. +# Don't include anything here that includes llvm headers. set(HEADER_FILES AddImageChecks.h AddParameterChecks.h + AlignLoads.h AllocationBoundsInference.h ApplySplit.h Argument.h @@ -254,26 +257,25 @@ set(HEADER_FILES BoundsInference.h BoundSmallAllocations.h Buffer.h - CSE.h - CanonicalizeGPUVars.h Closure.h CodeGen_ARM.h CodeGen_C.h CodeGen_GPU_Dev.h CodeGen_GPU_Host.h - CodeGen_Hexagon.h CodeGen_LLVM.h CodeGen_MIPS.h CodeGen_OpenCL_Dev.h CodeGen_Metal_Dev.h CodeGen_OpenGL_Dev.h CodeGen_OpenGLCompute_Dev.h + CodeGen_Posix.h CodeGen_PowerPC.h CodeGen_PTX_Dev.h - CodeGen_Posix.h CodeGen_X86.h ConciseCasts.h CPlusPlusMangle.h + CSE.h + CanonicalizeGPUVars.h Debug.h DebugArguments.h DebugToFile.h @@ -300,29 +302,31 @@ set(HEADER_FILES Generator.h HexagonOffload.h HexagonOptimize.h - IR.h - IREquality.h - IRMatch.h - IRMutator.h - IROperator.h - IRPrinter.h - IRVisitor.h + runtime/HalideRuntime.h + runtime/HalideBuffer.h ImageParam.h InferArguments.h - Interval.h InjectHostDevBufferCopies.h InjectOpenGLIntrinsics.h Inline.h InlineReductions.h IntegerDivisionTable.h + Interval.h Introspection.h IntrusivePtr.h + IREquality.h + IR.h + IRMatch.h + IRMutator.h + IROperator.h + IRPrinter.h + IRVisitor.h JITModule.h - LLVM_Output.h - LLVM_Runtime_Linker.h Lambda.h Lerp.h LICM.h + LLVM_Output.h + LLVM_Runtime_Linker.h LoopCarry.h Lower.h MainPage.h @@ -332,20 +336,19 @@ set(HEADER_FILES ModulusRemainder.h Monotonic.h ObjectInstanceRegistry.h - OutputImageParam.h Outputs.h + OutputImageParam.h ParallelRVar.h - Param.h Parameter.h + Param.h PartitionLoops.h Pipeline.h - PrintLoopNest.h Prefetch.h Profiling.h Qualify.h - RDom.h Random.h RealizationOrder.h + RDom.h Reduction.h RegionCosts.h RemoveDeadAllocations.h @@ -382,16 +385,17 @@ set(HEADER_FILES VectorizeLoops.h WrapCalls.h WrapExternStages.h - runtime/HalideRuntime.h - runtime/HalideBuffer.h ) file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/include") file(TO_NATIVE_PATH "${CMAKE_BINARY_DIR}/include/" NATIVE_INCLUDE_PATH) add_custom_command(OUTPUT "${CMAKE_BINARY_DIR}/include/Halide.h" - COMMAND build_halide_h ${HEADER_FILES} > "${NATIVE_INCLUDE_PATH}Halide.h" + COMMAND build_halide_h ${HEADER_FILES} HalideFooter.h > "${NATIVE_INCLUDE_PATH}Halide.h" WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}" DEPENDS build_halide_h ${HEADER_FILES}) +add_custom_target( + HalideIncludes + DEPENDS "${CMAKE_BINARY_DIR}/include/Halide.h") foreach (i ${RUNTIME_HEADER_FILES}) configure_file(runtime/${i} "${CMAKE_BINARY_DIR}/include" COPYONLY) @@ -529,7 +533,6 @@ add_library(Halide ${HALIDE_LIBRARY_TYPE} VectorizeLoops.cpp WrapCalls.cpp WrapExternStages.cpp - "${CMAKE_BINARY_DIR}/include/Halide.h" ${HEADER_FILES} ${INITIAL_MODULES} ) @@ -542,6 +545,7 @@ set_target_properties(Halide PROPERTIES VISIBILITY_INLINES_HIDDEN 1) add_dependencies(Halide binary2cpp build_halide_h + HalideIncludes ) # List of LLVM Components required From fa813baab1d2e1d73ed57336b0ce02696540a759 Mon Sep 17 00:00:00 2001 From: Steven Johnson Date: Mon, 13 Nov 2017 11:42:02 -0800 Subject: [PATCH 3/6] Don't put halide.tgz into distrib dir --- CMakeLists.txt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e1a51f502432..b1543a28953a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -482,15 +482,17 @@ foreach(F ${FILES}) 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 "${DISTRIB_DIR}/halide.zip" --format=zip "${DISTRIB_DIR}") + COMMAND cmake -E tar -cf "${NATIVE_ARCHIVE_DIR}/halide.zip" --format=zip "${NATIVE_DISTRIB_DIR}") else() add_custom_target(halide_archive DEPENDS build_distrib_dir - COMMAND cmake -E tar -czf "${DISTRIB_DIR}/halide.tgz" "${DISTRIB_DIR}") + COMMAND cmake -E tar -czf "${NATIVE_ARCHIVE_DIR}/halide.tgz" "${NATIVE_DISTRIB_DIR}") endif() add_custom_target(distrib) From ed21ce790819368f5f4db7c008a03913a1545775 Mon Sep 17 00:00:00 2001 From: Steven Johnson Date: Mon, 13 Nov 2017 12:49:56 -0800 Subject: [PATCH 4/6] Fix ambiguous dep for Halide.h --- CMakeLists.txt | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b1543a28953a..f2803f3b6c3e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -373,6 +373,10 @@ 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) @@ -397,6 +401,9 @@ function(add_to_distrib TARGET DIR) DEPENDS "${TARGET}") _make_distrib_subdir("${NAME}" "${DIR}") endif() + if("${args_DEPS}") + add_dependencies("${NAME}" "${args_DEPS}") + endif() add_dependencies(build_distrib_dir "${NAME}") endfunction() @@ -408,8 +415,8 @@ else() endif() # ---- Header files -add_dependencies(build_distrib_dir HalideIncludes) -add_to_distrib("${CMAKE_BINARY_DIR}/include/Halide.h" include) +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_SOURCE_DIR}/include/HalideRuntime*.h") foreach(F ${FILES}) From 3355c9b0cb22c1b6ab248afaf0f5c4448149568e Mon Sep 17 00:00:00 2001 From: Steven Johnson Date: Mon, 13 Nov 2017 12:55:05 -0800 Subject: [PATCH 5/6] Once more, because CMake is the devil --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f2803f3b6c3e..7514c1f81578 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -401,8 +401,8 @@ function(add_to_distrib TARGET DIR) DEPENDS "${TARGET}") _make_distrib_subdir("${NAME}" "${DIR}") endif() - if("${args_DEPS}") - add_dependencies("${NAME}" "${args_DEPS}") + if(NOT "${args_DEPS}" STREQUAL "") + add_dependencies("${NAME}" ${args_DEPS}) endif() add_dependencies(build_distrib_dir "${NAME}") endfunction() From 4f87323b3b7aa922881c9ffb443754c2342c302f Mon Sep 17 00:00:00 2001 From: Steven Johnson Date: Fri, 17 Nov 2017 16:53:07 -0800 Subject: [PATCH 6/6] fix distrib includes --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 89841acfb153..8a272b032831 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -424,7 +424,7 @@ endif() 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_SOURCE_DIR}/include/HalideRuntime*.h") +file(GLOB FILES "${CMAKE_BINARY_DIR}/include/HalideRuntime*.h") foreach(F ${FILES}) add_to_distrib(${F} include) endforeach()