Skip to content

Commit

Permalink
Allow to compile using external libclc libraries (#1366)
Browse files Browse the repository at this point in the history
This is important to be able to cross compile clspv.

LLVM supports cross-compilation, but the recent work on libclc that
lead to being able to build the libraries in-tree does not support
cross-compilation.

Thus we need to allow build systems to use libraries built outside of
clspv to cross-compile (It will be the case for ChromeOS).
  • Loading branch information
rjodinchr authored May 30, 2024
1 parent 1994f9e commit 86dc06b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
38 changes: 23 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,13 @@ if (${EXTERNAL_LLVM} EQUAL 1)
if (NOT DEFINED CLSPV_LLVM_BINARY_DIR)
message(FATAL_ERROR "External LLVM requires CLSPV_LLVM_BINARY_DIR to be specified")
endif()
if (NOT DEFINED CLSPV_LIBCLC_SOURCE_DIR)
message(FATAL_ERROR "External LLVM requires CLSPV_LIBCLC_SOURCE_DIR to be specified")
endif()
if (NOT DEFINED CLSPV_LLVM_CMAKE_MODULES_DIR)
message(FATAL_ERROR "External LLVM requires CLSPV_LLVM_CMAKE_MODULES_DIR to be specified")
if (NOT DEFINED CLSPV_EXTERNAL_LIBCLC_DIR)
if (NOT DEFINED CLSPV_LIBCLC_SOURCE_DIR)
message(FATAL_ERROR "External LLVM requires CLSPV_LIBCLC_SOURCE_DIR to be specified")
endif()
if (NOT DEFINED CLSPV_LLVM_CMAKE_MODULES_DIR)
message(FATAL_ERROR "External LLVM requires CLSPV_LLVM_CMAKE_MODULES_DIR to be specified")
endif()
endif()
else()
# Setup to use the LLVM monorepo.
Expand All @@ -126,12 +128,14 @@ else()
set(CLSPV_CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/llvm/clang)
endif()

if (NOT DEFINED CLSPV_LIBCLC_SOURCE_DIR)
set(CLSPV_LIBCLC_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/llvm/libclc)
endif()
if (NOT DEFINED CLSPV_EXTERNAL_LIBCLC_DIR)
if (NOT DEFINED CLSPV_LIBCLC_SOURCE_DIR)
set(CLSPV_LIBCLC_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/llvm/libclc)
endif()

if (NOT DEFINED CLSPV_LLVM_CMAKE_MODULES_DIR)
set(CLSPV_LLVM_CMAKE_MODULES_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/llvm/cmake/Modules)
if (NOT DEFINED CLSPV_LLVM_CMAKE_MODULES_DIR)
set(CLSPV_LLVM_CMAKE_MODULES_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/llvm/cmake/Modules)
endif()
endif()

use_component(${CLSPV_LLVM_SOURCE_DIR})
Expand All @@ -157,8 +161,10 @@ else()
include(HandleLLVMOptions)
endif()

include(${CLSPV_LLVM_CMAKE_MODULES_DIR}/LLVMVersion.cmake)
set(LLVM_VERSION ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH})
if (NOT DEFINED CLSPV_EXTERNAL_LIBCLC_DIR)
include(${CLSPV_LLVM_CMAKE_MODULES_DIR}/LLVMVersion.cmake)
set(LLVM_VERSION ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH})
endif()

set(CLSPV_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})

Expand Down Expand Up @@ -194,9 +200,11 @@ elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror-unused-variable -Werror-switch")
endif()

set(LIBCLC_TARGETS_TO_BUILD clspv--;clspv64--)
string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}" "" CLSPV_LIBCLC_SUFFIX "${CLSPV_LIBCLC_SOURCE_DIR}")
add_subdirectory(${CLSPV_LIBCLC_SOURCE_DIR} EXCLUDE_FROM_ALL)
if (NOT DEFINED CLSPV_EXTERNAL_LIBCLC_DIR)
set(LIBCLC_TARGETS_TO_BUILD clspv--;clspv64--)
string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}" "" CLSPV_LIBCLC_SUFFIX "${CLSPV_LIBCLC_SOURCE_DIR}")
add_subdirectory(${CLSPV_LIBCLC_SOURCE_DIR} EXCLUDE_FROM_ALL)
endif()

# Bring in our cmake folder
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/cmake)
Expand Down
20 changes: 16 additions & 4 deletions cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,36 @@ add_custom_target(clspv_builtin_library
add_custom_target(clspv64_builtin_library
DEPENDS ${CLSPV64_LIBRARY_OUTPUT_FILE})

if (NOT DEFINED CLSPV_EXTERNAL_LIBCLC_DIR)
set(CLSPV_LIBRARY_INPUT_FILE ${CLSPV_BINARY_DIR}/${CLSPV_LIBCLC_SUFFIX}/clspv--.bc)
set(CLSPV64_LIBRARY_INPUT_FILE ${CLSPV_BINARY_DIR}/${CLSPV_LIBCLC_SUFFIX}/clspv64--.bc)
set(CLSPV_LIBRARY_INPUT_DEP prepare-clspv--.bc)
set(CLSPV64_LIBRARY_INPUT_DEP prepare-clspv64--.bc)
else()
set(CLSPV_LIBRARY_INPUT_FILE ${CLSPV_EXTERNAL_LIBCLC_DIR}/clspv--.bc)
set(CLSPV64_LIBRARY_INPUT_FILE ${CLSPV_EXTERNAL_LIBCLC_DIR}/clspv64--.bc)
set(CLSPV_LIBRARY_INPUT_DEP ${CLSPV_LIBRARY_INPUT_FILE})
set(CLSPV64_LIBRARY_INPUT_DEP ${CLSPV64_LIBRARY_INPUT_FILE})
endif()

add_custom_command(
OUTPUT ${CLSPV_LIBRARY_OUTPUT_FILE}
COMMAND ${Python3_EXECUTABLE} ${BAKE_FILE_PYTHON_FILE}
--input-header-file=${CLSPV_BINARY_DIR}/${CLSPV_LIBCLC_SUFFIX}/clspv--.bc
--input-header-file=${CLSPV_LIBRARY_INPUT_FILE}
--output-file=${CLSPV_LIBRARY_OUTPUT_FILE}
--header-var=${CLSPV_LIBRARY_DATA_VAR_NAME}
--header-size=${CLSPV_LIBRARY_SIZE_VAR_NAME}
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
DEPENDS ${BAKE_FILE_PYTHON_FILE} prepare-clspv--.bc
DEPENDS ${BAKE_FILE_PYTHON_FILE} ${CLSPV_LIBRARY_INPUT_DEP}
)

add_custom_command(
OUTPUT ${CLSPV64_LIBRARY_OUTPUT_FILE}
COMMAND ${Python3_EXECUTABLE} ${BAKE_FILE_PYTHON_FILE}
--input-header-file=${CLSPV_BINARY_DIR}/${CLSPV_LIBCLC_SUFFIX}/clspv64--.bc
--input-header-file=${CLSPV64_LIBRARY_INPUT_FILE}
--output-file=${CLSPV64_LIBRARY_OUTPUT_FILE}
--header-var=${CLSPV64_LIBRARY_DATA_VAR_NAME}
--header-size=${CLSPV64_LIBRARY_SIZE_VAR_NAME}
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
DEPENDS ${BAKE_FILE_PYTHON_FILE} prepare-clspv64--.bc
DEPENDS ${BAKE_FILE_PYTHON_FILE} ${CLSPV64_LIBRARY_INPUT_DEP}
)

0 comments on commit 86dc06b

Please sign in to comment.