Skip to content

Commit dadb301

Browse files
authored
Add SPIRV_TOOLS_EXPORT to public C++ API (KhronosGroup#5591)
In contrast to the C API, the C++ API did not have symbol visibility specified. An application using the C++ API would fail to link against a shared SPIRV-Tools library built with `-fvisibility=hidden`. Mark all classes in the public `.hpp` files with `SPIRV_TOOLS_EXPORT`. Add `SPIRV_TOOLS_LOCAL` to hide nested structs containing implementation details. Signed-off-by: Sven van Haastregt <sven.vanhaastregt@arm.com>
1 parent 53c0736 commit dadb301

File tree

5 files changed

+27
-21
lines changed

5 files changed

+27
-21
lines changed

include/spirv-tools/libspirv.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,19 @@ extern "C" {
3333
#else
3434
#define SPIRV_TOOLS_EXPORT __declspec(dllimport)
3535
#endif
36+
#define SPIRV_TOOLS_LOCAL
3637
#else
3738
#if defined(SPIRV_TOOLS_IMPLEMENTATION)
3839
#define SPIRV_TOOLS_EXPORT __attribute__((visibility("default")))
40+
#define SPIRV_TOOLS_LOCAL __attribute__((visibility("hidden")))
3941
#else
4042
#define SPIRV_TOOLS_EXPORT
43+
#define SPIRV_TOOLS_LOCAL
4144
#endif
4245
#endif
4346
#else
4447
#define SPIRV_TOOLS_EXPORT
48+
#define SPIRV_TOOLS_LOCAL
4549
#endif
4650

4751
// Helpers

include/spirv-tools/libspirv.hpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ using InstructionParser =
3737
std::function<spv_result_t(const spv_parsed_instruction_t& instruction)>;
3838

3939
// C++ RAII wrapper around the C context object spv_context.
40-
class Context {
40+
class SPIRV_TOOLS_EXPORT Context {
4141
public:
4242
// Constructs a context targeting the given environment |env|.
4343
//
@@ -73,7 +73,7 @@ class Context {
7373
};
7474

7575
// A RAII wrapper around a validator options object.
76-
class ValidatorOptions {
76+
class SPIRV_TOOLS_EXPORT ValidatorOptions {
7777
public:
7878
ValidatorOptions() : options_(spvValidatorOptionsCreate()) {}
7979
~ValidatorOptions() { spvValidatorOptionsDestroy(options_); }
@@ -163,7 +163,7 @@ class ValidatorOptions {
163163
};
164164

165165
// A C++ wrapper around an optimization options object.
166-
class OptimizerOptions {
166+
class SPIRV_TOOLS_EXPORT OptimizerOptions {
167167
public:
168168
OptimizerOptions() : options_(spvOptimizerOptionsCreate()) {}
169169
~OptimizerOptions() { spvOptimizerOptionsDestroy(options_); }
@@ -205,7 +205,7 @@ class OptimizerOptions {
205205
};
206206

207207
// A C++ wrapper around a reducer options object.
208-
class ReducerOptions {
208+
class SPIRV_TOOLS_EXPORT ReducerOptions {
209209
public:
210210
ReducerOptions() : options_(spvReducerOptionsCreate()) {}
211211
~ReducerOptions() { spvReducerOptionsDestroy(options_); }
@@ -236,7 +236,7 @@ class ReducerOptions {
236236
};
237237

238238
// A C++ wrapper around a fuzzer options object.
239-
class FuzzerOptions {
239+
class SPIRV_TOOLS_EXPORT FuzzerOptions {
240240
public:
241241
FuzzerOptions() : options_(spvFuzzerOptionsCreate()) {}
242242
~FuzzerOptions() { spvFuzzerOptionsDestroy(options_); }
@@ -283,7 +283,7 @@ class FuzzerOptions {
283283
// provides methods for assembling, disassembling, and validating.
284284
//
285285
// Instances of this class provide basic thread-safety guarantee.
286-
class SpirvTools {
286+
class SPIRV_TOOLS_EXPORT SpirvTools {
287287
public:
288288
enum {
289289
// Default assembling option used by assemble():
@@ -388,7 +388,8 @@ class SpirvTools {
388388
bool IsValid() const;
389389

390390
private:
391-
struct Impl; // Opaque struct for holding the data fields used by this class.
391+
struct SPIRV_TOOLS_LOCAL
392+
Impl; // Opaque struct for holding the data fields used by this class.
392393
std::unique_ptr<Impl> impl_; // Unique pointer to implementation data.
393394
};
394395

include/spirv-tools/linker.hpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
namespace spvtools {
2626

27-
class LinkerOptions {
27+
class SPIRV_TOOLS_EXPORT LinkerOptions {
2828
public:
2929
// Returns whether a library or an executable should be produced by the
3030
// linking phase.
@@ -84,14 +84,15 @@ class LinkerOptions {
8484
// * Some entry points were defined multiple times;
8585
// * Some imported symbols did not have an exported counterpart;
8686
// * Possibly other reasons.
87-
spv_result_t Link(const Context& context,
88-
const std::vector<std::vector<uint32_t>>& binaries,
89-
std::vector<uint32_t>* linked_binary,
90-
const LinkerOptions& options = LinkerOptions());
91-
spv_result_t Link(const Context& context, const uint32_t* const* binaries,
92-
const size_t* binary_sizes, size_t num_binaries,
93-
std::vector<uint32_t>* linked_binary,
94-
const LinkerOptions& options = LinkerOptions());
87+
SPIRV_TOOLS_EXPORT spv_result_t
88+
Link(const Context& context, const std::vector<std::vector<uint32_t>>& binaries,
89+
std::vector<uint32_t>* linked_binary,
90+
const LinkerOptions& options = LinkerOptions());
91+
SPIRV_TOOLS_EXPORT spv_result_t
92+
Link(const Context& context, const uint32_t* const* binaries,
93+
const size_t* binary_sizes, size_t num_binaries,
94+
std::vector<uint32_t>* linked_binary,
95+
const LinkerOptions& options = LinkerOptions());
9596

9697
} // namespace spvtools
9798

include/spirv-tools/linter.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace spvtools {
2424
// provides a method for linting.
2525
//
2626
// Instances of this class provides basic thread-safety guarantee.
27-
class Linter {
27+
class SPIRV_TOOLS_EXPORT Linter {
2828
public:
2929
explicit Linter(spv_target_env env);
3030

@@ -40,7 +40,7 @@ class Linter {
4040
bool Run(const uint32_t* binary, size_t binary_size);
4141

4242
private:
43-
struct Impl;
43+
struct SPIRV_TOOLS_LOCAL Impl;
4444
std::unique_ptr<Impl> impl_;
4545
};
4646
} // namespace spvtools

include/spirv-tools/optimizer.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ struct DescriptorSetAndBinding;
3737
// provides methods for registering optimization passes and optimizing.
3838
//
3939
// Instances of this class provides basic thread-safety guarantee.
40-
class Optimizer {
40+
class SPIRV_TOOLS_EXPORT Optimizer {
4141
public:
4242
// The token for an optimization pass. It is returned via one of the
4343
// Create*Pass() standalone functions at the end of this header file and
4444
// consumed by the RegisterPass() method. Tokens are one-time objects that
4545
// only support move; copying is not allowed.
4646
struct PassToken {
47-
struct Impl; // Opaque struct for holding internal data.
47+
struct SPIRV_TOOLS_LOCAL Impl; // Opaque struct for holding internal data.
4848

4949
PassToken(std::unique_ptr<Impl>);
5050

@@ -239,7 +239,7 @@ class Optimizer {
239239
Optimizer& SetValidateAfterAll(bool validate);
240240

241241
private:
242-
struct Impl; // Opaque struct for holding internal data.
242+
struct SPIRV_TOOLS_LOCAL Impl; // Opaque struct for holding internal data.
243243
std::unique_ptr<Impl> impl_; // Unique pointer to internal data.
244244
};
245245

0 commit comments

Comments
 (0)