Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow build with external dependencies #42

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,34 @@ project (shady C)
include(ExternalProject)
include(FetchContent)

find_package(Git)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
option(BUILD_RUNTIME "Whether to build runtime components" ON)
option(BUILD_SAMPLES "Whether to build built-in demo applications" ON)
option(SHADY_USE_FETCHCONTENT "Use FetchContent to grab json-c" ON)
option(SHADY_WIN32_FIX_PARTIAL_LLVM_INSTALL "If you install LLVM on windows, it doesn't come with header files. This fixes it" ON)

option(EXTERNAL_JSON_C "Build using external JSON-C library" OFF)
option(EXTERNAL_JSON_C_INCLUDE "Include directory of external JSON-C headers" )
option(EXTERNAL_SPIRV_HEADERS "Build using external SPIRV headers" OFF)

if (MSVC)
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
endif()

if (WIN32)
set(BUILD_SHARED_LIBS OFF)
set(BUILD_SHARED_LIBS OFF)
endif()

add_subdirectory(SPIRV-Headers)
if (NOT ${EXTERNAL_SPIRV_HEADERS})
add_subdirectory(SPIRV-Headers)
endif ()

if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.24" AND ${SHADY_USE_FETCHCONTENT})
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.24" AND ${SHADY_USE_FETCHCONTENT}
AND NOT ${EXTERNAL_JSON_C})
FetchContent_Declare(
json-c
GIT_REPOSITORY https://github.com/json-c/json-c
Expand All @@ -49,6 +56,7 @@ if(NOT ${LLVM_FOUND} AND WIN32 AND ${SHADY_WIN32_FIX_PARTIAL_LLVM_INSTALL})
if(${clang_exe} STREQUAL "clang_exe-NOTFOUND")
message(STATUS "Win32: Installed LLVM not found")
else()
find_package(Git)
execute_process(COMMAND ${clang_exe} --version OUTPUT_VARIABLE clang_status)
string(REGEX MATCH "InstalledDir: (.*)[\r\n]" match ${clang_status})
file(TO_CMAKE_PATH "${CMAKE_MATCH_1}/../" LLVM_DIR)
Expand Down Expand Up @@ -85,9 +93,11 @@ endif()

include(GNUInstallDirs)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(SHADY_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin)

set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
Expand All @@ -103,15 +113,17 @@ add_subdirectory(zhady)

include(CTest)
if (BUILD_TESTING)
add_subdirectory(test)
add_subdirectory(test)
endif()

add_subdirectory(samples)
if (BUILD_SAMPLES)
add_subdirectory(samples)
endif()

include(CMakePackageConfigHelpers)

if (TARGET vcc)
add_subdirectory(vcc-std)
add_subdirectory(vcc-std)
endif ()

install(EXPORT shady_export_set DESTINATION share/cmake/shady/ NAMESPACE shady:: FILE shady-targets.cmake)
Expand Down
6 changes: 5 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
add_subdirectory(common)
add_subdirectory(shady)
add_subdirectory(runtime)

if (${BUILD_RUNTIME})
add_subdirectory(runtime)
endif ()

add_subdirectory(driver)
add_subdirectory(frontend)
add_subdirectory(backend)
2 changes: 1 addition & 1 deletion src/common/dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ KeyHash shd_hash(const void* data, size_t size) {
unsigned int hash = 0;
unsigned int i = 0;

for (i = 0; i < size; data++, i++)
for (i = 0; i < size; data_chars++, i++)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was kind of quite lucky. The compilation of the framework failed for Windows because the compiler couldn't estimate the exact size of a single const void element in this line. So I was able to find this.

{
hash *= fnv_prime;
hash ^= (*data_chars);
Expand Down
9 changes: 6 additions & 3 deletions src/shady/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
add_subdirectory(generator)

# :( see https://gitlab.kitware.com/cmake/cmake/-/issues/18399
# set_source_files_properties(${CMAKE_SOURCE_DIR}/include/shady/generated_grammar.h PROPERTIES GENERATED TRUE)
#set_property(SOURCE ${CMAKE_SOURCE_DIR}/include/shady/generated_grammar.h PROPERTY GENERATED TRUE)
# set_source_files_properties(${SHADY_SOURCE_DIR}/include/shady/generated_grammar.h PROPERTIES GENERATED TRUE)
#set_property(SOURCE ${SHADY_SOURCE_DIR}/include/shady/generated_grammar.h PROPERTY GENERATED TRUE)

add_subdirectory(api)

Expand Down Expand Up @@ -56,7 +56,10 @@ add_subdirectory(internal)

target_link_libraries(shady PRIVATE "api")
target_link_libraries(shady PRIVATE "common")
target_link_libraries(shady PRIVATE "$<BUILD_INTERFACE:m>")

if (NOT MSVC)
target_link_libraries(shady PRIVATE "$<BUILD_INTERFACE:m>")
endif ()

install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/shady DESTINATION include)
#install(TARGETS shady EXPORT shady_export_set ARCHIVE DESTINATION ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY})
10 changes: 4 additions & 6 deletions src/shady/api/generator_grammar.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,11 @@ void generate_node_ctor(Growy* g, json_object* src, json_object* nodes) {
shd_growy_append_formatted(g, " {\n");
shd_growy_append_formatted(g, "\tNode node;\n");
shd_growy_append_formatted(g, "\tmemset((void*) &node, 0, sizeof(Node));\n");
shd_growy_append_formatted(g, "\tnode = (Node) {\n");
shd_growy_append_formatted(g, "\t\t.arena = arena,\n");
shd_growy_append_formatted(g, "\t\t.tag = %s_TAG,\n", name);
shd_growy_append_formatted(g, "\tnode.arena = arena;\n");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of this change ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The project I'm using it in is compiling with C++20 which throws an error if not all fields are included in such an initializer list statement. It seems to only trigger in the generated code and I figured it was easier to simply use a syntax that causes less friction between different compilers.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I've been doing a lot of deep refactoring work, and I've not looked at windows builds in a hot second. The C++ header inclusion issue is something I'll try to keep in mind!

shd_growy_append_formatted(g, "\tnode.tag = %s_TAG;\n", name);
if (ops)
shd_growy_append_formatted(g, "\t\t.payload.%s = payload,\n", snake_name);
shd_growy_append_formatted(g, "\t\t.type = NULL,\n");
shd_growy_append_formatted(g, "\t};\n");
shd_growy_append_formatted(g, "\tnode.payload.%s = payload;\n", snake_name);
shd_growy_append_formatted(g, "\tnode.type = NULL;\n");
shd_growy_append_formatted(g, "\treturn _shd_create_node_helper(arena, node, NULL);\n");
shd_growy_append_formatted(g, "}\n");

Expand Down
14 changes: 12 additions & 2 deletions src/shady/generator/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
find_package(json-c REQUIRED)

if (NOT ${EXTERNAL_JSON_C})
find_package(json-c REQUIRED)
set(json_c_library json-c::json-c)
else ()
set(json_c_library json-c)
endif ()

add_library(generator_common STATIC generator.c generator_common.c json_apply.c)
target_link_libraries(generator_common PUBLIC common json-c::json-c)
target_link_libraries(generator_common PUBLIC common ${json_c_library})
target_include_directories(generator_common PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

if (${EXTERNAL_JSON_C})
target_include_directories(generator_common SYSTEM PUBLIC ${EXTERNAL_JSON_C_INCLUDE})
endif ()

add_executable(import_spv_defs import_spv_defs.c)
target_link_libraries(import_spv_defs PUBLIC common generator_common)

Expand Down
10 changes: 5 additions & 5 deletions zhady/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
find_package(SWIG)
find_package(JNI)
find_package(Java)
find_package(SWIG QUIET)
find_package(JNI QUIET)
find_package(Java QUIET)

if (NOT SWIG_FOUND)
message("SWIG not found. Skipping Java bindings.")
elseif (NOT SWIG_FOUND)
elseif (NOT JNI_FOUND)
message("JNI not found. Skipping Java bindings.")
elseif (NOT SWIG_FOUND)
elseif (NOT Java_FOUND)
message("Java not found. Skipping Java bindings.")
else()
option(SHADY_ENABLE_JAVA_BINDINGS "Allows using shady (and if enabled, Vcc) in JVM applications" ON)
Expand Down
Loading