Skip to content

Commit e73e0af

Browse files
committed
add handling DLL imports/exports for builtin resource targets, have optional _SHARED_ argument in ADD_CUSTOM_BUILTIN_RESOURCES
1 parent 1ecd112 commit e73e0af

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

src/nbl/builtin/builtinHeaderGen.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
resourcesFile = sys.argv[3]
1919
resourcesNamespace = sys.argv[4]
2020
guardSuffix = sys.argv[5]
21+
isSharedLibrary = sys.argv[6]
2122

2223
file = open(resourcesFile, 'r')
2324
resourcePaths = file.readlines()
@@ -32,6 +33,22 @@
3233
outp.write("#include <string>\n")
3334
outp.write("#include <unordered_map>\n")
3435
outp.write("#include <utility>\n#include <nbl/core/string/StringLiteral.h>\n\n")
36+
37+
if isSharedLibrary:
38+
outp.write("#if defined(__NBL_BUILDING_TARGET__) // currently compiling the target, this define is passed through the commandline\n")
39+
outp.write("#if defined(_MSC_VER)\n")
40+
outp.write("#define NBL_API2 __declspec(dllexport)\n")
41+
outp.write("#elif defined(__GNUC__)\n")
42+
outp.write('#define NBL_API2 __attribute__ ((visibility ("default")))' + "\n")
43+
outp.write("#endif\n")
44+
outp.write("#else\n")
45+
outp.write("#if defined(_MSC_VER)\n")
46+
outp.write("#define NBL_API2 __declspec(dllimport)\n")
47+
outp.write("#else\n")
48+
outp.write("#define NBL_API2\n")
49+
outp.write("#endif\n")
50+
outp.write("#endif\n\n")
51+
3552
outp.write("namespace " + resourcesNamespace + " { \n")
3653
outp.write("\t\ttemplate<nbl::core::StringLiteral Path>\n")
3754
outp.write("\t\tconst std::pair<const uint8_t*, size_t> get_resource();\n")
@@ -41,11 +58,17 @@
4158
itemData = z.split(',')
4259
x = itemData[0].rstrip()
4360

44-
outp.write('\n\t\ttemplate<> const std::pair<const uint8_t*, size_t> get_resource<NBL_CORE_UNIQUE_STRING_LITERAL_TYPE("%s")>();' % x)
61+
if isSharedLibrary:
62+
outp.write('\n\t\ttemplate<> NBL_API2 const std::pair<const uint8_t*, size_t> get_resource<NBL_CORE_UNIQUE_STRING_LITERAL_TYPE("%s")>();' % x)
63+
else:
64+
outp.write('\n\t\ttemplate<> const std::pair<const uint8_t*, size_t> get_resource<NBL_CORE_UNIQUE_STRING_LITERAL_TYPE("%s")>();' % x)
4565

4666
if len(itemData) > 1:
4767
for i in range(1, len(itemData)):
48-
outp.write('\n\t\ttemplate<> const std::pair<const uint8_t*, size_t> get_resource<NBL_CORE_UNIQUE_STRING_LITERAL_TYPE("%s")>();' % itemData[i].rstrip())
68+
if isSharedLibrary:
69+
outp.write('\n\t\ttemplate<> NBL_API2 const std::pair<const uint8_t*, size_t> get_resource<NBL_CORE_UNIQUE_STRING_LITERAL_TYPE("%s")>();' % itemData[i].rstrip())
70+
else:
71+
outp.write('\n\t\ttemplate<> const std::pair<const uint8_t*, size_t> get_resource<NBL_CORE_UNIQUE_STRING_LITERAL_TYPE("%s")>();' % itemData[i].rstrip())
4972

5073
outp.write("\n\t}")
5174
outp.write("\n#endif // _" + guardSuffix + "_BUILTINRESOURCEDATA_H_")

src/nbl/builtin/utils.cmake

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ endmacro()
3232
# _NAMESPACE_ is a C++ namespace builtin resources will be wrapped into
3333
# _OUTPUT_INCLUDE_SEARCH_DIRECTORY_ is an absolute path to output directory for builtin resources header files which will be a search directory for generated headers outputed to ${_OUTPUT_HEADER_DIRECTORY_}/${_NAMESPACE_PREFIX_} where namespace prefix is the namespace turned into a path
3434
# _OUTPUT_SOURCE_DIRECTORY_ is an absolute path to output directory for builtin resources source files
35+
# _STATIC_ optional last argument is a bool, if true then add_library will use STATIC, SHARED otherwise. Pay attention that MSVC runtime is controlled by NBL_DYNAMIC_MSVC_RUNTIME which is not an argument of this function
3536
#
3637
# As an example one could list a resource as following
3738
# LIST_BUILTIN_RESOURCE(SOME_RESOURCES_TO_EMBED "glsl/blit/default_compute_normalization.comp")
@@ -114,21 +115,35 @@ function(ADD_CUSTOM_BUILTIN_RESOURCES _TARGET_NAME_ _BUNDLE_NAME_ _BUNDLE_SEARCH
114115

115116
set(NBL_BUILTIN_RESOURCES_HEADER "${_OUTPUT_HEADER_DIRECTORY_}/${NBL_BS_HEADER_FILENAME}")
116117
set(NBL_BUILTIN_RESOURCE_DATA_SOURCE "${_OUTPUT_SOURCE_DIRECTORY_}/${NBL_BS_DATA_SOURCE_FILENAME}")
118+
119+
if("${ARGV7}" STREQUAL "SHARED")
120+
set(_LIB_TYPE_ SHARED)
121+
set(_SHARED_ True)
122+
else()
123+
set(_LIB_TYPE_ STATIC)
124+
set(_SHARED_ False)
125+
endif()
117126

118127
add_custom_command(
119128
OUTPUT "${NBL_BUILTIN_RESOURCES_HEADER}" "${NBL_BUILTIN_RESOURCE_DATA_SOURCE}"
120-
COMMAND "${_Python3_EXECUTABLE}" "${NBL_BUILTIN_HEADER_GEN_PY}" "${NBL_BUILTIN_RESOURCES_HEADER}" "${_BUNDLE_SEARCH_DIRECTORY_}/${_BUNDLE_ARCHIVE_ABSOLUTE_PATH_}" "${NBL_RESOURCES_LIST_FILE}" "${_NAMESPACE_}" "${_GUARD_SUFFIX_}"
129+
COMMAND "${_Python3_EXECUTABLE}" "${NBL_BUILTIN_HEADER_GEN_PY}" "${NBL_BUILTIN_RESOURCES_HEADER}" "${_BUNDLE_SEARCH_DIRECTORY_}/${_BUNDLE_ARCHIVE_ABSOLUTE_PATH_}" "${NBL_RESOURCES_LIST_FILE}" "${_NAMESPACE_}" "${_GUARD_SUFFIX_}" "${_SHARED_}"
121130
COMMAND "${_Python3_EXECUTABLE}" "${NBL_BUILTIN_DATA_GEN_PY}" "${NBL_BUILTIN_RESOURCE_DATA_SOURCE}" "${_BUNDLE_SEARCH_DIRECTORY_}/${_BUNDLE_ARCHIVE_ABSOLUTE_PATH_}" "${NBL_RESOURCES_LIST_FILE}" "${_NAMESPACE_}" "${NBL_BS_HEADER_FILENAME}"
122131
COMMENT "Generating built-in resources"
123132
DEPENDS ${NBL_DEPENDENCY_FILES}
124133
VERBATIM
125134
)
126135

127-
add_library(${_TARGET_NAME_} STATIC
136+
add_library(${_TARGET_NAME_} ${_LIB_TYPE_}
128137
"${NBL_BUILTIN_RESOURCES_HEADER}"
129138
"${NBL_BUILTIN_RESOURCE_DATA_SOURCE}"
130139
)
131140

141+
if(_LIB_TYPE_ STREQUAL SHARED)
142+
target_compile_definitions(${_TARGET_NAME_}
143+
PRIVATE __NBL_BUILDING_TARGET__
144+
)
145+
endif()
146+
132147
target_include_directories(${_TARGET_NAME_} PUBLIC
133148
"${NBL_ROOT_PATH}/include"
134149
"${_OUTPUT_HEADER_DIRECTORY_}"

0 commit comments

Comments
 (0)