From 04a123a742437cf320479ecd9298d07dbc887256 Mon Sep 17 00:00:00 2001 From: czoido Date: Fri, 17 Feb 2023 15:33:44 +0100 Subject: [PATCH 1/5] extend users presets example --- examples/tools/README.md | 2 + .../extend_own_cmake_presets/run_example.py | 85 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/run_example.py diff --git a/examples/tools/README.md b/examples/tools/README.md index de7b70c9..18bb56fb 100644 --- a/examples/tools/README.md +++ b/examples/tools/README.md @@ -4,6 +4,8 @@ - CMakeToolchain: [Building your project using CMakePresets](cmake/cmake_toolchain/local_flow_cmake_presets/). [Docs](https://docs.conan.io/en/2.0/examples/tools/cmake/cmake_toolchain/build_project_cmake_presets.html) +- CMakeToolchain: [Extending your CMakePresets with Conan generated ones](cmake/cmake_toolchain/extend_own_cmake_presets/). [Docs](https://docs.conan.io/en/2.0/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets.html) + ### [tools.files](files) - Learn how [patch sources](files/patches/). [Docs](https://docs.conan.io/en/2.0/examples/tools/files/patches/patch_sources.html) diff --git a/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/run_example.py b/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/run_example.py new file mode 100644 index 00000000..7653dbc1 --- /dev/null +++ b/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/run_example.py @@ -0,0 +1,85 @@ +import os +import platform +import textwrap + +from test.examples_tools import run, replace + +from conan import conan_version + +print("- CMakeToolchain: Extending your CMakePresets with Conan generated ones -") + +run("conan new -d name=foo -d version=1.0 cmake_exe") + +replace("conanfile.py", "tc.generate()", "tc.user_presets_path = 'ConanPresets.json'\n tc.generate()") + +cmake_presets = textwrap.dedent(""" + { + "version": 4, + "include": ["./ConanPresets.json"], + "configurePresets": [ + { + "name": "default", + "displayName": "multi config", + "inherits": "conan-default" + }, + { + "name": "release", + "displayName": "release single config", + "inherits": "conan-release" + }, + { + "name": "debug", + "displayName": "debug single config", + "inherits": "conan-debug" + } + ], + "buildPresets": [ + { + "name": "multi-release", + "configurePreset": "default", + "configuration": "Release", + "inherits": "conan-release" + }, + { + "name": "multi-debug", + "configurePreset": "default", + "configuration": "Debug", + "inherits": "conan-debug" + }, + { + "name": "release", + "configurePreset": "release", + "configuration": "Release", + "inherits": "conan-release" + }, + { + "name": "debug", + "configurePreset": "debug", + "configuration": "Debug", + "inherits": "conan-debug" + } + ] + }""") + +with open("CMakePresets.json", "w") as f: + f.write(cmake_presets) + + +run("conan install .") +run("conan install . -s build_type=Debug") + +if platform.system() == "Windows": + run(f"cmake --preset default") + run(f"cmake --build --preset release") + run(f"cmake --build --preset debug") +else: + run(f"cmake --preset release") + run(f"cmake --build --preset release") + run(f"cmake --preset debug") + run(f"cmake --build --preset debug") + +output = run(str(os.path.join("build", "Release", "foo"))) +assert "Hello World Release!" in output + +output = run(str(os.path.join("build", "Debug", "foo"))) +assert "Hello World Debug!" in output From 08d7f3cf88346785f44e04298b62c75fd3fa77b7 Mon Sep 17 00:00:00 2001 From: czoido Date: Fri, 17 Feb 2023 15:52:29 +0100 Subject: [PATCH 2/5] wip --- .../extend_own_cmake_presets/CMakeLists.txt | 14 +++ .../CMakePresets.json | 48 +++++++++ .../extend_own_cmake_presets/conanfile.py | 42 ++++++++ .../extend_own_cmake_presets/run_example.py | 57 ----------- .../extend_own_cmake_presets/src/foo.cpp | 99 +++++++++++++++++++ .../extend_own_cmake_presets/src/foo.h | 10 ++ .../extend_own_cmake_presets/src/main.cpp | 5 + .../test_package/conanfile.py | 14 +++ 8 files changed, 232 insertions(+), 57 deletions(-) create mode 100644 examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/CMakeLists.txt create mode 100644 examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/CMakePresets.json create mode 100644 examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/conanfile.py create mode 100644 examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/src/foo.cpp create mode 100644 examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/src/foo.h create mode 100644 examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/src/main.cpp create mode 100644 examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/test_package/conanfile.py diff --git a/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/CMakeLists.txt b/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/CMakeLists.txt new file mode 100644 index 00000000..43322e4d --- /dev/null +++ b/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.15) +project(foo CXX) + + + +add_executable(foo src/foo.cpp src/main.cpp) + + + +install(TARGETS foo DESTINATION "." + RUNTIME DESTINATION bin + ARCHIVE DESTINATION lib + LIBRARY DESTINATION lib + ) diff --git a/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/CMakePresets.json b/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/CMakePresets.json new file mode 100644 index 00000000..bdcb7315 --- /dev/null +++ b/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/CMakePresets.json @@ -0,0 +1,48 @@ + +{ +"version": 4, +"include": ["./ConanPresets.json"], +"configurePresets": [ + { + "name": "default", + "displayName": "multi config", + "inherits": "conan-default" + }, + { + "name": "release", + "displayName": "release single config", + "inherits": "conan-release" + }, + { + "name": "debug", + "displayName": "debug single config", + "inherits": "conan-debug" + } +], +"buildPresets": [ + { + "name": "multi-release", + "configurePreset": "default", + "configuration": "Release", + "inherits": "conan-release" + }, + { + "name": "multi-debug", + "configurePreset": "default", + "configuration": "Debug", + "inherits": "conan-debug" + }, + { + "name": "release", + "configurePreset": "release", + "configuration": "Release", + "inherits": "conan-release" + }, + { + "name": "debug", + "configurePreset": "debug", + "configuration": "Debug", + "inherits": "conan-debug" + } +] +} \ No newline at end of file diff --git a/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/conanfile.py b/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/conanfile.py new file mode 100644 index 00000000..c33ca134 --- /dev/null +++ b/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/conanfile.py @@ -0,0 +1,42 @@ +from conan import ConanFile +from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout, CMakeDeps + + +class fooRecipe(ConanFile): + name = "foo" + version = "1.0" + package_type = "application" + + # Optional metadata + license = "" + author = " " + url = "" + description = "" + topics = ("", "", "") + + # Binary configuration + settings = "os", "compiler", "build_type", "arch" + + # Sources are located in the same place as this recipe, copy them to the recipe + exports_sources = "CMakeLists.txt", "src/*" + + def layout(self): + cmake_layout(self) + + def generate(self): + deps = CMakeDeps(self) + deps.generate() + tc = CMakeToolchain(self) + tc.user_presets_path = 'ConanPresets.json' + tc.generate() + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + + def package(self): + cmake = CMake(self) + cmake.install() + + diff --git a/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/run_example.py b/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/run_example.py index 7653dbc1..6d751d16 100644 --- a/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/run_example.py +++ b/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/run_example.py @@ -8,63 +8,6 @@ print("- CMakeToolchain: Extending your CMakePresets with Conan generated ones -") -run("conan new -d name=foo -d version=1.0 cmake_exe") - -replace("conanfile.py", "tc.generate()", "tc.user_presets_path = 'ConanPresets.json'\n tc.generate()") - -cmake_presets = textwrap.dedent(""" - { - "version": 4, - "include": ["./ConanPresets.json"], - "configurePresets": [ - { - "name": "default", - "displayName": "multi config", - "inherits": "conan-default" - }, - { - "name": "release", - "displayName": "release single config", - "inherits": "conan-release" - }, - { - "name": "debug", - "displayName": "debug single config", - "inherits": "conan-debug" - } - ], - "buildPresets": [ - { - "name": "multi-release", - "configurePreset": "default", - "configuration": "Release", - "inherits": "conan-release" - }, - { - "name": "multi-debug", - "configurePreset": "default", - "configuration": "Debug", - "inherits": "conan-debug" - }, - { - "name": "release", - "configurePreset": "release", - "configuration": "Release", - "inherits": "conan-release" - }, - { - "name": "debug", - "configurePreset": "debug", - "configuration": "Debug", - "inherits": "conan-debug" - } - ] - }""") - -with open("CMakePresets.json", "w") as f: - f.write(cmake_presets) - - run("conan install .") run("conan install . -s build_type=Debug") diff --git a/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/src/foo.cpp b/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/src/foo.cpp new file mode 100644 index 00000000..90212391 --- /dev/null +++ b/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/src/foo.cpp @@ -0,0 +1,99 @@ +#include +#include "foo.h" + + + +void foo(){ + + + #ifdef NDEBUG + std::cout << "foo/1.0: Hello World Release!\n"; + #else + std::cout << "foo/1.0: Hello World Debug!\n"; + #endif + + // ARCHITECTURES + #ifdef _M_X64 + std::cout << " foo/1.0: _M_X64 defined\n"; + #endif + + #ifdef _M_IX86 + std::cout << " foo/1.0: _M_IX86 defined\n"; + #endif + + #ifdef _M_ARM64 + std::cout << " foo/1.0: _M_ARM64 defined\n"; + #endif + + #if __i386__ + std::cout << " foo/1.0: __i386__ defined\n"; + #endif + + #if __x86_64__ + std::cout << " foo/1.0: __x86_64__ defined\n"; + #endif + + #if __aarch64__ + std::cout << " foo/1.0: __aarch64__ defined\n"; + #endif + + // Libstdc++ + #if defined _GLIBCXX_USE_CXX11_ABI + std::cout << " foo/1.0: _GLIBCXX_USE_CXX11_ABI "<< _GLIBCXX_USE_CXX11_ABI << "\n"; + #endif + + // COMPILER VERSIONS + #if _MSC_VER + std::cout << " foo/1.0: _MSC_VER" << _MSC_VER<< "\n"; + #endif + + #if _MSVC_LANG + std::cout << " foo/1.0: _MSVC_LANG" << _MSVC_LANG<< "\n"; + #endif + + #if __cplusplus + std::cout << " foo/1.0: __cplusplus" << __cplusplus<< "\n"; + #endif + + #if __INTEL_COMPILER + std::cout << " foo/1.0: __INTEL_COMPILER" << __INTEL_COMPILER<< "\n"; + #endif + + #if __GNUC__ + std::cout << " foo/1.0: __GNUC__" << __GNUC__<< "\n"; + #endif + + #if __GNUC_MINOR__ + std::cout << " foo/1.0: __GNUC_MINOR__" << __GNUC_MINOR__<< "\n"; + #endif + + #if __clang_major__ + std::cout << " foo/1.0: __clang_major__" << __clang_major__<< "\n"; + #endif + + #if __clang_minor__ + std::cout << " foo/1.0: __clang_minor__" << __clang_minor__<< "\n"; + #endif + + #if __apple_build_version__ + std::cout << " foo/1.0: __apple_build_version__" << __apple_build_version__<< "\n"; + #endif + + // SUBSYSTEMS + + #if __MSYS__ + std::cout << " foo/1.0: __MSYS__" << __MSYS__<< "\n"; + #endif + + #if __MINGW32__ + std::cout << " foo/1.0: __MINGW32__" << __MINGW32__<< "\n"; + #endif + + #if __MINGW64__ + std::cout << " foo/1.0: __MINGW64__" << __MINGW64__<< "\n"; + #endif + + #if __CYGWIN__ + std::cout << " foo/1.0: __CYGWIN__" << __CYGWIN__<< "\n"; + #endif +} diff --git a/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/src/foo.h b/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/src/foo.h new file mode 100644 index 00000000..fbd3aca5 --- /dev/null +++ b/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/src/foo.h @@ -0,0 +1,10 @@ +#pragma once + + +#ifdef _WIN32 + #define FOO_EXPORT __declspec(dllexport) +#else + #define FOO_EXPORT +#endif + +FOO_EXPORT void foo(); diff --git a/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/src/main.cpp b/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/src/main.cpp new file mode 100644 index 00000000..a102f04c --- /dev/null +++ b/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/src/main.cpp @@ -0,0 +1,5 @@ +#include "foo.h" + +int main() { + foo(); +} diff --git a/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/test_package/conanfile.py b/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/test_package/conanfile.py new file mode 100644 index 00000000..514eadc9 --- /dev/null +++ b/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/test_package/conanfile.py @@ -0,0 +1,14 @@ +import os +from conan import ConanFile +from conan.tools.build import can_run + + +class fooTestConan(ConanFile): + settings = "os", "compiler", "build_type", "arch" + + def requirements(self): + self.requires(self.tested_reference_str) + + def test(self): + if can_run(self): + self.run("foo", env="conanrun") From fc147cd35a50ac34119d4273a5663871c88faeff Mon Sep 17 00:00:00 2001 From: czoido Date: Fri, 17 Feb 2023 15:53:46 +0100 Subject: [PATCH 3/5] simplify --- .../CMakePresets.json | 65 +++++++------- .../extend_own_cmake_presets/src/foo.cpp | 87 ------------------- 2 files changed, 32 insertions(+), 120 deletions(-) diff --git a/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/CMakePresets.json b/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/CMakePresets.json index bdcb7315..3801322d 100644 --- a/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/CMakePresets.json +++ b/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/CMakePresets.json @@ -1,48 +1,47 @@ - { -"version": 4, -"include": ["./ConanPresets.json"], -"configurePresets": [ + "version": 4, + "include": ["./ConanPresets.json"], + "configurePresets": [ { - "name": "default", - "displayName": "multi config", - "inherits": "conan-default" + "name": "default", + "displayName": "multi config", + "inherits": "conan-default" }, { - "name": "release", - "displayName": "release single config", - "inherits": "conan-release" + "name": "release", + "displayName": "release single config", + "inherits": "conan-release" }, { - "name": "debug", - "displayName": "debug single config", - "inherits": "conan-debug" + "name": "debug", + "displayName": "debug single config", + "inherits": "conan-debug" } -], -"buildPresets": [ + ], + "buildPresets": [ { - "name": "multi-release", - "configurePreset": "default", - "configuration": "Release", - "inherits": "conan-release" + "name": "multi-release", + "configurePreset": "default", + "configuration": "Release", + "inherits": "conan-release" }, { - "name": "multi-debug", - "configurePreset": "default", - "configuration": "Debug", - "inherits": "conan-debug" + "name": "multi-debug", + "configurePreset": "default", + "configuration": "Debug", + "inherits": "conan-debug" }, { - "name": "release", - "configurePreset": "release", - "configuration": "Release", - "inherits": "conan-release" + "name": "release", + "configurePreset": "release", + "configuration": "Release", + "inherits": "conan-release" }, { - "name": "debug", - "configurePreset": "debug", - "configuration": "Debug", - "inherits": "conan-debug" + "name": "debug", + "configurePreset": "debug", + "configuration": "Debug", + "inherits": "conan-debug" } -] -} \ No newline at end of file + ] +} diff --git a/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/src/foo.cpp b/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/src/foo.cpp index 90212391..21af2d92 100644 --- a/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/src/foo.cpp +++ b/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/src/foo.cpp @@ -4,96 +4,9 @@ void foo(){ - - #ifdef NDEBUG std::cout << "foo/1.0: Hello World Release!\n"; #else std::cout << "foo/1.0: Hello World Debug!\n"; #endif - - // ARCHITECTURES - #ifdef _M_X64 - std::cout << " foo/1.0: _M_X64 defined\n"; - #endif - - #ifdef _M_IX86 - std::cout << " foo/1.0: _M_IX86 defined\n"; - #endif - - #ifdef _M_ARM64 - std::cout << " foo/1.0: _M_ARM64 defined\n"; - #endif - - #if __i386__ - std::cout << " foo/1.0: __i386__ defined\n"; - #endif - - #if __x86_64__ - std::cout << " foo/1.0: __x86_64__ defined\n"; - #endif - - #if __aarch64__ - std::cout << " foo/1.0: __aarch64__ defined\n"; - #endif - - // Libstdc++ - #if defined _GLIBCXX_USE_CXX11_ABI - std::cout << " foo/1.0: _GLIBCXX_USE_CXX11_ABI "<< _GLIBCXX_USE_CXX11_ABI << "\n"; - #endif - - // COMPILER VERSIONS - #if _MSC_VER - std::cout << " foo/1.0: _MSC_VER" << _MSC_VER<< "\n"; - #endif - - #if _MSVC_LANG - std::cout << " foo/1.0: _MSVC_LANG" << _MSVC_LANG<< "\n"; - #endif - - #if __cplusplus - std::cout << " foo/1.0: __cplusplus" << __cplusplus<< "\n"; - #endif - - #if __INTEL_COMPILER - std::cout << " foo/1.0: __INTEL_COMPILER" << __INTEL_COMPILER<< "\n"; - #endif - - #if __GNUC__ - std::cout << " foo/1.0: __GNUC__" << __GNUC__<< "\n"; - #endif - - #if __GNUC_MINOR__ - std::cout << " foo/1.0: __GNUC_MINOR__" << __GNUC_MINOR__<< "\n"; - #endif - - #if __clang_major__ - std::cout << " foo/1.0: __clang_major__" << __clang_major__<< "\n"; - #endif - - #if __clang_minor__ - std::cout << " foo/1.0: __clang_minor__" << __clang_minor__<< "\n"; - #endif - - #if __apple_build_version__ - std::cout << " foo/1.0: __apple_build_version__" << __apple_build_version__<< "\n"; - #endif - - // SUBSYSTEMS - - #if __MSYS__ - std::cout << " foo/1.0: __MSYS__" << __MSYS__<< "\n"; - #endif - - #if __MINGW32__ - std::cout << " foo/1.0: __MINGW32__" << __MINGW32__<< "\n"; - #endif - - #if __MINGW64__ - std::cout << " foo/1.0: __MINGW64__" << __MINGW64__<< "\n"; - #endif - - #if __CYGWIN__ - std::cout << " foo/1.0: __CYGWIN__" << __CYGWIN__<< "\n"; - #endif } From abdb62c480450c95c8575f6f22f615a0c79dc5bd Mon Sep 17 00:00:00 2001 From: czoido Date: Fri, 17 Feb 2023 16:14:58 +0100 Subject: [PATCH 4/5] fix --- .../extend_own_cmake_presets/run_example.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/run_example.py b/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/run_example.py index 6d751d16..7f74a14d 100644 --- a/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/run_example.py +++ b/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/run_example.py @@ -21,8 +21,13 @@ run(f"cmake --preset debug") run(f"cmake --build --preset debug") -output = run(str(os.path.join("build", "Release", "foo"))) -assert "Hello World Release!" in output - -output = run(str(os.path.join("build", "Debug", "foo"))) -assert "Hello World Debug!" in output +if platform.system() == "Windows": + output = run("build\\Release\\foo.exe") + assert "Hello World Release!" in output + output = run("build\\Debug\\foo.exe") + assert "Hello World Debug!" in output +else: + output = run(str(os.path.join("build", "Release", "foo"))) + assert "Hello World Release!" in output + output = run(str(os.path.join("build", "Debug", "foo"))) + assert "Hello World Debug!" in output From 892ae3187c14d604e644d00a48d116672b755347 Mon Sep 17 00:00:00 2001 From: czoido Date: Fri, 17 Feb 2023 16:35:23 +0100 Subject: [PATCH 5/5] fix --- .../cmake_toolchain/extend_own_cmake_presets/run_example.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/run_example.py b/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/run_example.py index 7f74a14d..04cfb74b 100644 --- a/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/run_example.py +++ b/examples/tools/cmake/cmake_toolchain/extend_own_cmake_presets/run_example.py @@ -13,8 +13,8 @@ if platform.system() == "Windows": run(f"cmake --preset default") - run(f"cmake --build --preset release") - run(f"cmake --build --preset debug") + run(f"cmake --build --preset multi-release") + run(f"cmake --build --preset multi-debug") else: run(f"cmake --preset release") run(f"cmake --build --preset release")