diff --git a/BUILD.gn b/BUILD.gn index dec1ca1140..b0d9468124 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -132,31 +132,115 @@ template("dawn_generator") { } } +############################################################################### +# Template to produce static and shared versions of Dawn's libraries +############################################################################### + +# Template that produces static and shared versions of the same library. +# - The shared version exports symbols and has dependent import the symbols +# as libname.so with target name libname_shared +# - The static library doesn't export symbols nor make dependents import them +# - The libname target is an alias for libname_shared. This is mostly to keep +# the GN convention that target_type(name) defines a "name" target. +# +# The DEFINE_PREFIX must be provided and must match the respective "_export.h" +# file. +# +# Example usage: +# +# dawn_static_and_shared_library("my_library") { +# // my_library_export.h must use the MY_LIBRARY_IMPLEMENTATION and +# // MY_LIBRARY_SHARED_LIBRARY macros. +# DEFINE_PREFIX = "MY_LIBRARY" +# +# sources = [...] +# deps = [...] +# configs = [...] +# } +# +# executable("foo") { +# deps = [ ":my_library_shared" ] // or :my_library for the same effect +# } +template("dawn_static_and_shared_library") { + # Copy the target_name in the local scope so it doesn't get shadowed in the + # definition of targets. + libname = target_name + + # The config that will apply to dependents of the shared library so they know + # they should "import" the symbols + config("${libname}_shared_public_config") { + defines = [ "${invoker.DEFINE_PREFIX}_SHARED_LIBRARY" ] + + # Executable needs an rpath to find our shared libraries on OSX and Linux + if (is_mac) { + ldflags = [ + "-rpath", + "@executable_path/", + ] + } + if (is_linux) { + configs = [ "//build/config/gcc:rpath_for_built_shared_libraries" ] + } + } + + shared_library("${libname}_shared") { + output_name = libname + + # Copy all variables except "configs", which has a default value + forward_variables_from(invoker, "*", [ "configs" ]) + if (defined(invoker.configs)) { + configs += invoker.configs + } + + # Tell dependents where to find this shared library + if (is_mac) { + ldflags = [ + "-install_name", + "@rpath/${libname}.dylib", + ] + } + + # Use the config that makes the ${DEFINE_PREFIX}_EXPORT macro do something + if (!defined(public_configs)) { + public_configs = [] + } + public_configs += [ ":${libname}_shared_public_config" ] + + # Tell sources of this library to export the symbols (and not import) + if (!defined(defines)) { + defines = [] + } + defines += [ "${invoker.DEFINE_PREFIX}_IMPLEMENTATION" ] + } + + static_library("${libname}_static") { + output_name = libname + + # Copy all variables except "configs", which has a default value + forward_variables_from(invoker, "*", [ "configs" ]) + if (defined(invoker.configs)) { + configs += invoker.configs + } + } + + group(libname) { + public_deps = [ + ":${libname}_shared", + ] + } +} + ############################################################################### # Common dawn libraries and configs ############################################################################### -config("libdawn_public") { +config("dawn_public") { include_dirs = [ target_gen_dir, "src/include", ] } -# Executable needs an rpath to find our shared libraries on OSX and Linux -config("dawn_shared_library_public") { - if (is_mac) { - ldflags = [ - "-rpath", - "@executable_path/", - ] - } - - if (is_linux) { - configs = [ "//build/config/gcc:rpath_for_built_shared_libraries" ] - } -} - config("dawn_internal") { include_dirs = [ "src" ] @@ -181,10 +265,7 @@ config("dawn_internal") { defines += [ "DAWN_ENABLE_BACKEND_VULKAN" ] } - configs = [ - ":libdawn_public", - ":dawn_shared_library_public", - ] + configs = [ ":dawn_public" ] # Only internal Dawn targets can use this config, this means only targets in # this BUILD.gn file. @@ -220,7 +301,7 @@ static_library("dawn_common") { } ############################################################################### -# Dawn headers and libdawn.so +# Dawn headers ############################################################################### dawn_generator("dawn_headers_gen") { @@ -233,7 +314,7 @@ dawn_generator("dawn_headers_gen") { } source_set("dawn_headers") { - public_configs = [ ":libdawn_public" ] + public_configs = [ ":dawn_public" ] deps = [ ":dawn_headers_gen", ] @@ -246,6 +327,10 @@ source_set("dawn_headers") { ] } +############################################################################### +# libdawn +############################################################################### + dawn_generator("libdawn_gen") { target = "libdawn" outputs = [ @@ -254,11 +339,9 @@ dawn_generator("libdawn_gen") { ] } -config("libdawn_export") { - defines = [ "DAWN_IMPLEMENTATION" ] -} +dawn_static_and_shared_library("libdawn") { + DEFINE_PREFIX = "DAWN" -source_set("libdawn_sources") { public_deps = [ ":dawn_headers", ] @@ -267,31 +350,10 @@ source_set("libdawn_sources") { ":libdawn_gen", ] sources = get_target_outputs(":libdawn_gen") - - # Put the export config public so that dependents use the same declspec. - public_configs = [ ":libdawn_export" ] -} - -shared_library("libdawn") { - public_deps = [ - ":dawn_headers", - ] - - deps = [ - ":libdawn_sources", - ] - - # Tell dependents where to find this shared library - if (is_mac) { - ldflags = [ - "-install_name", - "@rpath/${target_name}.dylib", - ] - } } ############################################################################### -# libdawn_native.so +# libdawn_native ############################################################################### config("libdawn_native_internal") { @@ -303,10 +365,6 @@ config("libdawn_native_internal") { } } -config("libdawn_native_export") { - defines = [ "DAWN_NATIVE_IMPLEMENTATION" ] -} - dawn_generator("libdawn_native_utils_gen") { target = "dawn_native_utils" outputs = [ @@ -324,6 +382,7 @@ source_set("libdawn_native_headers") { public_deps = [ ":dawn_headers", ] + public_configs = [ ":dawn_public" ] sources = [ "src/include/dawn_native/DawnNative.h", "src/include/dawn_native/dawn_native_export.h", @@ -339,20 +398,16 @@ source_set("libdawn_native_headers") { } # The meat of the compilation for libdawn_native so that we can cheaply have -# shared_library / static_library / component versions of it. +# shared_library / static_library versions of it. It compiles all the files +# except those that define exported symbols. source_set("libdawn_native_sources") { deps = [ ":dawn_common", + ":libdawn_native_headers", ":libdawn_native_utils_gen", "${dawn_spirv_tools_dir}:spvtools_val", "third_party:spirv_cross", ] - public_deps = [ - ":libdawn_native_headers", - ] - - # Put the export config public so that dependents use the same declspec. - public_configs = [ ":libdawn_native_export" ] configs += [ ":libdawn_native_internal" ] libs = [] @@ -383,7 +438,6 @@ source_set("libdawn_native_sources") { "src/dawn_native/ComputePassEncoder.h", "src/dawn_native/ComputePipeline.cpp", "src/dawn_native/ComputePipeline.h", - "src/dawn_native/DawnNative.cpp", "src/dawn_native/Device.cpp", "src/dawn_native/Device.h", "src/dawn_native/Error.cpp", @@ -447,7 +501,6 @@ source_set("libdawn_native_sources") { "src/dawn_native/d3d12/CommandBufferD3D12.h", "src/dawn_native/d3d12/ComputePipelineD3D12.cpp", "src/dawn_native/d3d12/ComputePipelineD3D12.h", - "src/dawn_native/d3d12/D3D12Backend.cpp", "src/dawn_native/d3d12/DescriptorHeapAllocator.cpp", "src/dawn_native/d3d12/DescriptorHeapAllocator.h", "src/dawn_native/d3d12/DeviceD3D12.cpp", @@ -505,7 +558,6 @@ source_set("libdawn_native_sources") { "src/dawn_native/metal/Forward.h", "src/dawn_native/metal/InputStateMTL.h", "src/dawn_native/metal/InputStateMTL.mm", - "src/dawn_native/metal/MetalBackend.mm", "src/dawn_native/metal/PipelineLayoutMTL.h", "src/dawn_native/metal/PipelineLayoutMTL.mm", "src/dawn_native/metal/QueueMTL.h", @@ -531,7 +583,6 @@ source_set("libdawn_native_sources") { sources += [ "src/dawn_native/null/DeviceNull.cpp", "src/dawn_native/null/DeviceNull.h", - "src/dawn_native/null/NullBackend.cpp", ] } @@ -551,7 +602,6 @@ source_set("libdawn_native_sources") { "src/dawn_native/opengl/Forward.h", "src/dawn_native/opengl/InputStateGL.cpp", "src/dawn_native/opengl/InputStateGL.h", - "src/dawn_native/opengl/OpenGLBackend.cpp", "src/dawn_native/opengl/PersistentPipelineStateGL.cpp", "src/dawn_native/opengl/PersistentPipelineStateGL.h", "src/dawn_native/opengl/PipelineGL.cpp", @@ -621,7 +671,6 @@ source_set("libdawn_native_sources") { "src/dawn_native/vulkan/TextureVk.h", "src/dawn_native/vulkan/UtilsVulkan.cpp", "src/dawn_native/vulkan/UtilsVulkan.h", - "src/dawn_native/vulkan/VulkanBackend.cpp", "src/dawn_native/vulkan/VulkanError.cpp", "src/dawn_native/vulkan/VulkanError.h", "src/dawn_native/vulkan/VulkanFunctions.cpp", @@ -632,29 +681,47 @@ source_set("libdawn_native_sources") { } } -# The shared library for libdawn_native for use by samples, tests, etc. -shared_library("libdawn_native") { - deps = [ - ":libdawn_native_sources", - ] +# The static and shared libraries for libdawn_native. Most of the files are +# already compiled in libdawn_native_sources, but we still need to compile +# files defining exported symbols. +dawn_static_and_shared_library("libdawn_native") { + DEFINE_PREFIX = "DAWN_NATIVE" #Make headers publically visible public_deps = [ ":libdawn_native_headers", ] - public_configs = [ ":libdawn_public" ] - # Tell dependents where to find this shared library - if (is_mac) { - ldflags = [ - "-install_name", - "@rpath/${target_name}.dylib", - ] + deps = [ + ":libdawn_native_sources", + ":dawn_common", + ] + sources = [ + "src/dawn_native/DawnNative.cpp", + ] + configs = [ ":libdawn_native_internal" ] + + if (dawn_enable_d3d12) { + sources += [ "src/dawn_native/d3d12/D3D12Backend.cpp" ] + } + if (dawn_enable_metal) { + sources += [ "src/dawn_native/metal/MetalBackend.mm" ] + } + if (dawn_enable_null) { + sources += [ "src/dawn_native/null/NullBackend.cpp" ] + } + if (dawn_enable_opengl) { + sources += [ "src/dawn_native/opengl/OpenGLBackend.cpp" ] + deps += [ "third_party:glad" ] + } + if (dawn_enable_vulkan) { + sources += [ "src/dawn_native/vulkan/VulkanBackend.cpp" ] + deps += [ "third_party:vulkan_headers" ] } } ############################################################################### -# libdawn_wire.so +# libdawn_wire ############################################################################### # Public libdawn_wire headers so they can be publically visible for @@ -663,16 +730,13 @@ source_set("libdawn_wire_headers") { public_deps = [ ":dawn_headers", ] + public_configs = [ ":dawn_public" ] sources = [ "src/include/dawn_wire/Wire.h", "src/include/dawn_wire/dawn_wire_export.h", ] } -config("libdawn_wire_export") { - defines = [ "DAWN_WIRE_IMPLEMENTATION" ] -} - dawn_generator("libdawn_wire_gen") { target = "dawn_wire" outputs = [ @@ -684,14 +748,16 @@ dawn_generator("libdawn_wire_gen") { ] } -source_set("libdawn_wire_sources") { +dawn_static_and_shared_library("libdawn_wire") { + DEFINE_PREFIX = "DAWN_WIRE" + deps = [ ":dawn_common", ":libdawn_wire_gen", ":libdawn_wire_headers", ] - configs += [ ":dawn_internal" ] + configs = [ ":dawn_internal" ] sources = get_target_outputs(":libdawn_wire_gen") sources += [ "src/dawn_wire/WireCmd.h" ] @@ -699,30 +765,6 @@ source_set("libdawn_wire_sources") { public_deps = [ ":libdawn_wire_headers", ] - public_configs = [ ":libdawn_public" ] - - # Put the export config public so that dependents use the same declspec. - public_configs += [ ":libdawn_wire_export" ] -} - -shared_library("libdawn_wire") { - deps = [ - ":libdawn_wire_sources", - ] - - # Make headers publically visible - public_deps = [ - ":libdawn_wire_headers", - ] - public_configs = [ ":libdawn_public" ] - - # Tell dependents where to find this shared library - if (is_mac) { - ldflags = [ - "-install_name", - "@rpath/${target_name}.dylib", - ] - } } ############################################################################### @@ -806,6 +848,7 @@ test("dawn_unittests") { ":dawn_utils", ":libdawn", ":libdawn_native_sources", + ":libdawn_native", ":libdawn_wire", ":mock_dawn_gen", "third_party:gmock_and_gtest", diff --git a/generator/templates/apicpp.h b/generator/templates/apicpp.h index f1d732c2cc..133ec5c264 100644 --- a/generator/templates/apicpp.h +++ b/generator/templates/apicpp.h @@ -130,9 +130,9 @@ namespace dawn { {% macro render_cpp_method_declaration(type, method) %} {% set CppType = as_cppType(type.name) %} {% if method.return_type.name.concatcase() == "void" and type.is_builder -%} - {{CppType}} const& + DAWN_EXPORT {{CppType}} const& {%- else -%} - {{as_cppType(method.return_type.name)}} + DAWN_EXPORT {{as_cppType(method.return_type.name)}} {%- endif -%} {{" "}}{{method.name.CamelCase()}}( {%- for arg in method.arguments -%} @@ -149,7 +149,7 @@ namespace dawn { {% for type in by_category["object"] %} {% set CppType = as_cppType(type.name) %} {% set CType = as_cType(type.name) %} - class DAWN_EXPORT {{CppType}} : public ObjectBase<{{CppType}}, {{CType}}> { + class {{CppType}} : public ObjectBase<{{CppType}}, {{CType}}> { public: using ObjectBase::ObjectBase; using ObjectBase::operator=; @@ -160,8 +160,8 @@ namespace dawn { private: friend ObjectBase<{{CppType}}, {{CType}}>; - static void DawnReference({{CType}} handle); - static void DawnRelease({{CType}} handle); + static DAWN_EXPORT void DawnReference({{CType}} handle); + static DAWN_EXPORT void DawnRelease({{CType}} handle); }; {% endfor %} diff --git a/src/fuzzers/BUILD.gn b/src/fuzzers/BUILD.gn index 80ecb2f6b9..128cf18f1f 100644 --- a/src/fuzzers/BUILD.gn +++ b/src/fuzzers/BUILD.gn @@ -164,10 +164,8 @@ dawn_fuzzer_test("dawn_wire_server_and_frontend_fuzzer") { ] deps = [ - "${dawn_top_level}:libdawn_sources", - "${dawn_top_level}:libdawn_native_sources", - "${dawn_top_level}:libdawn_wire_sources", + "${dawn_top_level}:libdawn_static", + "${dawn_top_level}:libdawn_native_static", + "${dawn_top_level}:libdawn_wire_static", ] - - additional_configs = [ "${dawn_top_level}:dawn_shared_library_public" ] } diff --git a/src/include/dawn/dawn_export.h b/src/include/dawn/dawn_export.h index d00c8fdb85..354bcfff0f 100644 --- a/src/include/dawn/dawn_export.h +++ b/src/include/dawn/dawn_export.h @@ -15,18 +15,22 @@ #ifndef DAWN_EXPORT_H_ #define DAWN_EXPORT_H_ -#if defined(_WIN32) -# if defined(DAWN_IMPLEMENTATION) -# define DAWN_EXPORT __declspec(dllexport) -# else -# define DAWN_EXPORT __declspec(dllimport) -# endif -#else -# if defined(DAWN_IMPLEMENTATION) -# define DAWN_EXPORT __attribute__((visibility("default"))) -# else -# define DAWN_EXPORT -# endif -#endif +#if defined(DAWN_SHARED_LIBRARY) +# if defined(_WIN32) +# if defined(DAWN_IMPLEMENTATION) +# define DAWN_EXPORT __declspec(dllexport) +# else +# define DAWN_EXPORT __declspec(dllimport) +# endif +# else // defined(_WIN32) +# if defined(DAWN_IMPLEMENTATION) +# define DAWN_EXPORT __attribute__((visibility("default"))) +# else +# define DAWN_EXPORT +# endif +# endif // defined(_WIN32) +#else // defined(DAWN_SHARED_LIBRARY) +# define DAWN_EXPORT +#endif // defined(DAWN_SHARED_LIBRARY) #endif // DAWN_EXPORT_H_ diff --git a/src/include/dawn_native/dawn_native_export.h b/src/include/dawn_native/dawn_native_export.h index f258167954..ffbd9cc369 100644 --- a/src/include/dawn_native/dawn_native_export.h +++ b/src/include/dawn_native/dawn_native_export.h @@ -15,18 +15,22 @@ #ifndef DAWNNATIVE_EXPORT_H_ #define DAWNNATIVE_EXPORT_H_ -#if defined(_WIN32) -# if defined(DAWN_NATIVE_IMPLEMENTATION) -# define DAWN_NATIVE_EXPORT __declspec(dllexport) -# else -# define DAWN_NATIVE_EXPORT __declspec(dllimport) -# endif -#else -# if defined(DAWN_NATIVE_IMPLEMENTATION) -# define DAWN_NATIVE_EXPORT __attribute__((visibility("default"))) -# else -# define DAWN_NATIVE_EXPORT -# endif -#endif +#if defined(DAWN_NATIVE_SHARED_LIBRARY) +# if defined(_WIN32) +# if defined(DAWN_NATIVE_IMPLEMENTATION) +# define DAWN_NATIVE_EXPORT __declspec(dllexport) +# else +# define DAWN_NATIVE_EXPORT __declspec(dllimport) +# endif +# else // defined(_WIN32) +# if defined(DAWN_NATIVE_IMPLEMENTATION) +# define DAWN_NATIVE_EXPORT __attribute__((visibility("default"))) +# else +# define DAWN_NATIVE_EXPORT +# endif +# endif // defined(_WIN32) +#else // defined(DAWN_NATIVE_SHARED_LIBRARY) +# define DAWN_NATIVE_EXPORT +#endif // defined(DAWN_NATIVE_SHARED_LIBRARY) #endif // DAWNNATIVE_EXPORT_H_ diff --git a/src/include/dawn_wire/dawn_wire_export.h b/src/include/dawn_wire/dawn_wire_export.h index d22376266d..8043f618c1 100644 --- a/src/include/dawn_wire/dawn_wire_export.h +++ b/src/include/dawn_wire/dawn_wire_export.h @@ -15,18 +15,22 @@ #ifndef DAWNWIRE_EXPORT_H_ #define DAWNWIRE_EXPORT_H_ -#if defined(_WIN32) -# if defined(DAWN_WIRE_IMPLEMENTATION) -# define DAWN_WIRE_EXPORT __declspec(dllexport) -# else -# define DAWN_WIRE_EXPORT __declspec(dllimport) -# endif -#else -# if defined(DAWN_WIRE_IMPLEMENTATION) -# define DAWN_WIRE_EXPORT __attribute__((visibility("default"))) -# else -# define DAWN_WIRE_EXPORT -# endif -#endif +#if defined(DAWN_WIRE_SHARED_LIBRARY) +# if defined(_WIN32) +# if defined(DAWN_WIRE_IMPLEMENTATION) +# define DAWN_WIRE_EXPORT __declspec(dllexport) +# else +# define DAWN_WIRE_EXPORT __declspec(dllimport) +# endif +# else // defined(_WIN32) +# if defined(DAWN_WIRE_IMPLEMENTATION) +# define DAWN_WIRE_EXPORT __attribute__((visibility("default"))) +# else +# define DAWN_WIRE_EXPORT +# endif +# endif // defined(_WIN32) +#else // defined(DAWN_WIRE_SHARED_LIBRARY) +# define DAWN_WIRE_EXPORT +#endif // defined(DAWN_WIRE_SHARED_LIBRARY) #endif // DAWNWIRE_EXPORT_H_