The method can be overridden by setting the `method` key in the wrap file and always defaults to 'meson'. cmake.subproject() is still needed in case specific cmake options need to be passed. This also makes it easier to extend to other methods in the future e.g. cargo.pull/12284/head
parent
c0da998afa
commit
49e7e3b9cc
21 changed files with 188 additions and 27 deletions
@ -0,0 +1,12 @@ |
||||
## Automatic fallback to `cmake` subproject |
||||
|
||||
CMake subprojects have been supported for a while using the `cmake.subproject()` |
||||
module method. However until now it was not possible to use a CMake subproject |
||||
as fallback in a `dependency()` call. |
||||
|
||||
A wrap file can now specify the method used to build it by setting the `method` |
||||
key in the wrap file's first section. The method defaults to `meson`. |
||||
|
||||
Supported methods: |
||||
- `meson` requires `meson.build` file. |
||||
- `cmake` requires `CMakeLists.txt` file. [See details](Wrap-dependency-system-manual.md#cmake-wraps). |
@ -0,0 +1,10 @@ |
||||
#include <iostream> |
||||
#include <cmMod.hpp> |
||||
|
||||
using namespace std; |
||||
|
||||
int main(void) { |
||||
cmModClass obj("Hello"); |
||||
cout << obj.getStr() << endl; |
||||
return 0; |
||||
} |
@ -0,0 +1,30 @@ |
||||
project('cmakeSubTest', ['c', 'cpp']) |
||||
|
||||
# Fallback to a CMake subproject |
||||
sub_dep = dependency('cmModLib++') |
||||
exe1 = executable('main', ['main.cpp'], dependencies: [sub_dep]) |
||||
test('test1', exe1) |
||||
|
||||
# Subproject contains both meson.build and CMakeLists.txt. It should default |
||||
# to meson but wrap force cmake. |
||||
subproject('force_cmake') |
||||
|
||||
testcase expect_error('Wrap method \'notfound\' is not supported, must be one of: meson, cmake') |
||||
subproject('broken_method') |
||||
endtestcase |
||||
|
||||
# With method=meson we can't use cmake.subproject() |
||||
cmake = import('cmake') |
||||
testcase expect_error('Wrap method is \'meson\' but we are trying to configure it with cmake') |
||||
cmake.subproject('meson_method') |
||||
endtestcase |
||||
|
||||
# cmake.subproject() force cmake method even if meson.build exists. |
||||
testcase expect_error('Subproject exists but has no CMakeLists.txt file.') |
||||
cmake.subproject('meson_subp') |
||||
endtestcase |
||||
|
||||
# Without specifying the method it defaults to meson even if CMakeLists.txt exists. |
||||
testcase expect_error('Subproject exists but has no meson.build file.') |
||||
subproject('cmake_subp') |
||||
endtestcase |
@ -0,0 +1,2 @@ |
||||
[wrap-file] |
||||
method=notfound |
@ -0,0 +1,5 @@ |
||||
[wrap-file] |
||||
method = cmake |
||||
|
||||
[provide] |
||||
cmModLib++ = cmModLib___dep |
@ -0,0 +1,20 @@ |
||||
cmake_minimum_required(VERSION 3.5) |
||||
|
||||
project(cmMod) |
||||
set(CMAKE_CXX_STANDARD 14) |
||||
|
||||
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) |
||||
|
||||
# Test PCH support |
||||
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.16.0") |
||||
target_precompile_headers(cmModLib++ PRIVATE "cpp_pch.hpp") |
||||
endif() |
||||
|
||||
include(GenerateExportHeader) |
||||
generate_export_header(cmModLib++) |
@ -0,0 +1,15 @@ |
||||
#include "cmMod.hpp" |
||||
|
||||
using namespace std; |
||||
|
||||
#if MESON_MAGIC_FLAG != 21 |
||||
#error "Invalid MESON_MAGIC_FLAG (private)" |
||||
#endif |
||||
|
||||
cmModClass::cmModClass(string foo) { |
||||
str = foo + " World"; |
||||
} |
||||
|
||||
string cmModClass::getStr() const { |
||||
return str; |
||||
} |
@ -0,0 +1,18 @@ |
||||
#pragma once |
||||
|
||||
#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; |
||||
|
||||
public: |
||||
cmModClass(std::string foo); |
||||
|
||||
std::string getStr() const; |
||||
}; |
@ -0,0 +1,2 @@ |
||||
#include <vector> |
||||
#include <string> |
@ -0,0 +1,2 @@ |
||||
cmake_minimum_required(VERSION 3.5) |
||||
project(cmModDummy) |
@ -0,0 +1,2 @@ |
||||
[wrap-file] |
||||
method=cmake |
@ -0,0 +1,2 @@ |
||||
cmake_minimum_required(VERSION 3.5) |
||||
project(cmModBoth) |
@ -0,0 +1,4 @@ |
||||
project('both methods') |
||||
|
||||
# Ensure the meson method is not used. |
||||
notfound() |
@ -0,0 +1,2 @@ |
||||
[wrap-file] |
||||
method=meson |
@ -0,0 +1 @@ |
||||
project('dummy') |
Loading…
Reference in new issue