Skip to content

Commit

Permalink
cmake: fix definitions with interface libraries (fixes #7299)
Browse files Browse the repository at this point in the history
  • Loading branch information
mensinda authored and nirbheek committed Jun 13, 2020
1 parent e20f02b commit f542677
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 7 deletions.
13 changes: 6 additions & 7 deletions mesonbuild/cmake/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,6 @@ def postprocess(self, output_target_map: OutputTargetMap, root_src_dir: str, sub
tgt = trace.targets.get(self.cmake_name)
if tgt:
self.depends_raw = trace.targets[self.cmake_name].depends
if self.type.upper() == 'INTERFACE_LIBRARY':
props = tgt.properties

self.includes += props.get('INTERFACE_INCLUDE_DIRECTORIES', [])
self.public_compile_opts += props.get('INTERFACE_COMPILE_DEFINITIONS', [])
self.public_compile_opts += props.get('INTERFACE_COMPILE_OPTIONS', [])
self.link_flags += props.get('INTERFACE_LINK_OPTIONS', [])

# TODO refactor this copy paste from CMakeDependency for future releases
reg_is_lib = re.compile(r'^(-l[a-zA-Z0-9_]+|-l?pthread)$')
Expand All @@ -340,6 +333,12 @@ def postprocess(self, output_target_map: OutputTargetMap, root_src_dir: str, sub
libraries = []
mlog.debug(tgt)

if 'INTERFACE_INCLUDE_DIRECTORIES' in tgt.properties:
self.includes += [x for x in tgt.properties['INTERFACE_INCLUDE_DIRECTORIES'] if x]

if 'INTERFACE_LINK_OPTIONS' in tgt.properties:
self.link_flags += [x for x in tgt.properties['INTERFACE_LINK_OPTIONS'] if x]

if 'INTERFACE_COMPILE_DEFINITIONS' in tgt.properties:
self.public_compile_opts += ['-D' + re.sub('^-D', '', x) for x in tgt.properties['INTERFACE_COMPILE_DEFINITIONS'] if x]

Expand Down
2 changes: 2 additions & 0 deletions test cases/cmake/1 basic/subprojects/cmMod/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR})
add_definitions("-DDO_NOTHING_JUST_A_FLAG=1")

add_library(cmModLib++ SHARED cmMod.cpp)
target_compile_definitions(cmModLib++ PRIVATE MESON_MAGIC_FLAG=21)
target_compile_definitions(cmModLib++ INTERFACE MESON_MAGIC_FLAG=42)
include(GenerateExportHeader)
generate_export_header(cmModLib++)
4 changes: 4 additions & 0 deletions test cases/cmake/1 basic/subprojects/cmMod/cmMod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

using namespace std;

#if MESON_MAGIC_FLAG != 21
#error "Invalid MESON_MAGIC_FLAG (private)"
#endif

cmModClass::cmModClass(string foo) {
str = foo + " World";
}
Expand Down
4 changes: 4 additions & 0 deletions test cases/cmake/1 basic/subprojects/cmMod/cmMod.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include "cmmodlib++_export.h"
#include <string>

#if MESON_MAGIC_FLAG != 42 && MESON_MAGIC_FLAG != 21
#error "Invalid MESON_MAGIC_FLAG"
#endif

class CMMODLIB___EXPORT cmModClass {
private:
std::string str;
Expand Down
6 changes: 6 additions & 0 deletions test cases/cmake/10 header only/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@

using namespace std;

#define EXPECTED "Hello World compDef 42"

int main(void) {
cmModClass obj("Hello");
cout << obj.getStr() << endl;
if (obj.getStr() != EXPECTED) {
cerr << "Expected: '" << EXPECTED << "'" << endl;
return 1;
}
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ add_library(cmModLib INTERFACE)
set_target_properties(cmModLib PROPERTIES INTERFACE_COMPILE_OPTIONS "-DCMAKE_FLAG_MUST_BE_PRESENT")
target_include_directories(cmModLib INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_compile_definitions(cmModLib INTERFACE -DCMAKE_COMPILER_DEFINE_STR="compDef")
target_compile_definitions(cmModLib INTERFACE MESON_MAGIC_FLAG=42)
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@
#error "The flag CMAKE_FLAG_MUST_BE_PRESENT was not set"
#endif

#define xstr(s) str(s)
#define str(s) #s

class cmModClass {
private:
std::string str;
public:
cmModClass(std::string foo) {
str = foo + " World ";
str += CMAKE_COMPILER_DEFINE_STR;
str += ' ';
str += xstr(MESON_MAGIC_FLAG);
}

inline std::string getStr() const { return str; }
Expand Down

0 comments on commit f542677

Please sign in to comment.