cmake: Sanitize CMake names (fixes #6554)

pull/6585/head
Daniel Mensinger 5 years ago committed by Jussi Pakkanen
parent 97dd522476
commit 1f7e73fd5c
  1. 22
      mesonbuild/cmake/interpreter.py
  2. 6
      test cases/cmake/1 basic/meson.build
  3. 4
      test cases/cmake/1 basic/subprojects/cmMod/CMakeLists.txt
  4. 15
      test cases/cmake/1 basic/subprojects/cmMod/cmMod.hpp

@ -125,10 +125,13 @@ blacklist_link_libs = [
'advapi32.lib'
]
generated_target_name_prefix = 'cm_'
transfer_dependencies_from = ['header_only']
_cmake_name_regex = re.compile(r'[^_a-zA-Z0-9]')
def _sanitize_cmake_name(name: str) -> str:
name = _cmake_name_regex.sub('_', name)
return 'cm_' + name
class OutputTargetMap:
rm_so_version = re.compile(r'(\.[0-9]+)+$')
@ -234,8 +237,7 @@ class ConverterTarget:
self.override_options = []
# Convert the target name to a valid meson target name
self.name = self.name.replace('-', '_')
self.name = generated_target_name_prefix + self.name
self.name = _sanitize_cmake_name(self.name)
for i in target.files:
# Determine the meson language
@ -593,8 +595,7 @@ class ConverterCustomTarget:
self.depends = []
# Convert the target name to a valid meson target name
self.name = self.name.replace('-', '_')
self.name = generated_target_name_prefix + self.name
self.name = _sanitize_cmake_name(self.name)
def __repr__(self) -> str:
return '<{}: {} {}>'.format(self.__class__.__name__, self.name, self.outputs)
@ -1192,6 +1193,7 @@ class CMakeInterpreter:
root_cb.lines += [assign(tgt_var, function('custom_target', [tgt.name], tgt_kwargs))]
processed[tgt.name] = {'inc': None, 'src': None, 'dep': None, 'tgt': tgt_var, 'func': 'custom_target'}
name_map[tgt.cmake_name] = tgt.name
# Now generate the target function calls
for i in self.custom_targets:
@ -1208,7 +1210,7 @@ class CMakeInterpreter:
def target_info(self, target: str) -> T.Optional[T.Dict[str, str]]:
# Try resolving the target name
# start by checking if there is a 100% match (excluding the name prefix)
prx_tgt = generated_target_name_prefix + target
prx_tgt = _sanitize_cmake_name(target)
if prx_tgt in self.generated_targets:
return self.generated_targets[prx_tgt]
# check if there exists a name mapping
@ -1219,11 +1221,7 @@ class CMakeInterpreter:
return None
def target_list(self) -> T.List[str]:
prx_str = generated_target_name_prefix
prx_len = len(prx_str)
res = [x for x in self.generated_targets.keys()]
res = [x[prx_len:] if x.startswith(prx_str) else x for x in res]
return res
return list(self.internal_name_map.keys())
def _object_lib_workaround(self) -> bool:
return 'link' in self.linkers and self.backend_name.startswith('vs')

@ -3,10 +3,10 @@ project('cmakeSubTest', ['c', 'cpp'])
cm = import('cmake')
sub_pro = cm.subproject('cmMod')
sub_dep = sub_pro.dependency('cmModLib')
sub_dep = sub_pro.dependency('cmModLib++')
assert(sub_pro.target_list() == ['cmModLib'], 'There should be exactly one target')
assert(sub_pro.target_type('cmModLib') == 'shared_library', 'Target type should be shared_library')
assert(sub_pro.target_list() == ['cmModLib++'], 'There should be exactly one target')
assert(sub_pro.target_type('cmModLib++') == 'shared_library', 'Target type should be shared_library')
exe1 = executable('main', ['main.cpp'], dependencies: [sub_dep])
test('test1', exe1)

@ -7,6 +7,6 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR})
add_definitions("-DDO_NOTHING_JUST_A_FLAG=1")
add_library(cmModLib SHARED cmMod.cpp)
add_library(cmModLib++ SHARED cmMod.cpp)
include(GenerateExportHeader)
generate_export_header(cmModLib)
generate_export_header(cmModLib++)

@ -1,13 +1,14 @@
#pragma once
#include "cmmodlib++_export.h"
#include <string>
#include "cmmodlib_export.h"
class CMMODLIB_EXPORT cmModClass {
private:
std::string str;
public:
cmModClass(std::string foo);
class CMMODLIB___EXPORT cmModClass {
private:
std::string str;
std::string getStr() const;
public:
cmModClass(std::string foo);
std::string getStr() const;
};

Loading…
Cancel
Save