Skip to content

Commit

Permalink
Support using an external LLVM tree when embedding in another project
Browse files Browse the repository at this point in the history
When embedding the translator in another CMake project the existing
automatic detection for so-called external builds no longer works.

This change adds an LLVM_SPIRV_BUILD_EXTERNAL variable that users
can set to force the decision. If the variable isn't set, the
behaviour is detected as previously.

Signed-off-by: Kevin Petit <kevin.petit@arm.com>
  • Loading branch information
kpet authored and AlexeySotkin committed Dec 6, 2019
1 parent d09b6b6 commit b152e65
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
15 changes: 8 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ option(LLVM_SPIRV_INCLUDE_TESTS
"Generate build targets for the llvm-spirv lit tests."
${LLVM_INCLUDE_TESTS})

# check if we build inside llvm or not
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
set(BUILD_EXTERNAL YES)
if (NOT DEFINED LLVM_SPIRV_BUILD_EXTERNAL)
# check if we build inside llvm or not
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
set(LLVM_SPIRV_BUILD_EXTERNAL YES)
endif(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
endif (NOT DEFINED LLVM_SPIRV_BUILD_EXTERNAL)

if(LLVM_SPIRV_BUILD_EXTERNAL)
project(LLVM_SPIRV
VERSION
${LLVM_SPIRV_VERSION}
Expand Down Expand Up @@ -43,11 +48,7 @@ if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
include(AddLLVM)

message(STATUS "Found LLVM: ${LLVM_VERSION}")
else(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
set(BUILD_EXTERNAL NO)
endif(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)

if(BUILD_EXTERNAL)
find_program(CCACHE_EXE_FOUND ccache)
if(CCACHE_EXE_FOUND)
message(STATUS "Found ccache: ${CCACHE_EXE_FOUND}")
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,23 @@ make llvm-spirv -j`nproc`

### Build with pre-built LLVM

If you have a custom build(based on the latest version) of LLVM libraries you can link the translator against it.
If you have a custom build (based on the latest version) of LLVM libraries you
can link the translator against it.

```
git clone https://github.com/KhronosGroup/SPIRV-LLVM-Translator.git
mkdir SPIRV-LLVM-Translator/build && cd SPIRV-LLVM-Translator/build
cmake .. -DLLVM_DIR=<llvm_build_dir>/lib/cmake/llvm/
make llvm-spirv -j`nproc`
```

If the translator is used as part of another CMake project, you will need
to define `LLVM_SPIRV_BUILD_EXTERNAL`:

```
cmake .. -DLLVM_DIR=<llvm_build_dir>/lib/cmake/llvm/ -DLLVM_SPIRV_BUILD_EXTERNAL=YES
```

Where `llvm_build_dir` is the LLVM build directory.

### LLVM in-tree build
Expand Down
8 changes: 4 additions & 4 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ configure_lit_site_cfg(
${CMAKE_CURRENT_SOURCE_DIR}/lit.cfg.py
)

if(NOT BUILD_EXTERNAL)
if(NOT LLVM_SPIRV_BUILD_EXTERNAL)
set(LLVM_SPIRV_TEST_DEPS
FileCheck
count
Expand All @@ -63,7 +63,7 @@ if(NOT BUILD_EXTERNAL)
llvm-readobj
)
endif(NOT SPIRV_SKIP_DEBUG_INFO_TESTS)
endif(NOT BUILD_EXTERNAL)
endif(NOT LLVM_SPIRV_BUILD_EXTERNAL)


add_lit_testsuite(check-llvm-spirv "Running the LLVM-SPIRV regression tests"
Expand All @@ -77,7 +77,7 @@ add_lit_testsuite(check-llvm-spirv "Running the LLVM-SPIRV regression tests"

# to enable a custom test target on cmake below 3.11
# starting with 3.11 "test" is only reserved if ENABLE_TESTING(ON)
if(BUILD_EXTERNAL)
if(LLVM_SPIRV_BUILD_EXTERNAL)
cmake_policy(PUSH)
if(POLICY CMP0037 AND ${CMAKE_VERSION} VERSION_LESS "3.11.0")
cmake_policy(SET CMP0037 OLD)
Expand All @@ -87,4 +87,4 @@ if(BUILD_EXTERNAL)
check-llvm-spirv
)
cmake_policy(POP)
endif(BUILD_EXTERNAL)
endif(LLVM_SPIRV_BUILD_EXTERNAL)
2 changes: 1 addition & 1 deletion tools/llvm-spirv/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ add_llvm_tool(llvm-spirv
NO_INSTALL_RPATH
)

if (BUILD_EXTERNAL)
if (LLVM_SPIRV_BUILD_EXTERNAL)
target_link_libraries(llvm-spirv PRIVATE LLVMSPIRVLib)
endif()

Expand Down

0 comments on commit b152e65

Please sign in to comment.