├── .github └── workflows │ ├── linux.yml │ ├── macos.yml │ └── windows.yml ├── .gitignore ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── example ├── .gitignore ├── CMakeLists.txt ├── packages │ ├── common.lua │ ├── gflags.lua │ ├── glog.lua │ └── xmake.lua └── src │ └── main.cpp ├── scripts └── test-unix.sh └── xrepo.cmake /.github/workflows/linux.yml: -------------------------------------------------------------------------------- 1 | name: Linux 2 | 3 | on: 4 | pull_request: 5 | push: 6 | 7 | jobs: 8 | build: 9 | strategy: 10 | matrix: 11 | os: [ubuntu-latest] 12 | kind: [static, shared] 13 | 14 | runs-on: ${{ matrix.os }} 15 | 16 | steps: 17 | - uses: actions/checkout@v1 18 | 19 | - name: Installation 20 | run: | 21 | sudo apt-get install -y cmake 22 | sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 60 23 | sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 60 24 | sudo update-alternatives --install /usr/bin/cpp cpp /usr/bin/cpp-9 60 25 | sudo update-alternatives --set g++ /usr/bin/g++-9 26 | sudo update-alternatives --set gcc /usr/bin/gcc-9 27 | sudo update-alternatives --set cpp /usr/bin/cpp-9 28 | 29 | - name: Tests 30 | run: | 31 | ./scripts/test-unix.sh 32 | -------------------------------------------------------------------------------- /.github/workflows/macos.yml: -------------------------------------------------------------------------------- 1 | name: macOS 2 | 3 | on: 4 | pull_request: 5 | push: 6 | 7 | jobs: 8 | build: 9 | strategy: 10 | matrix: 11 | os: [macOS-latest] 12 | kind: [static, shared] 13 | 14 | runs-on: ${{ matrix.os }} 15 | 16 | steps: 17 | - uses: actions/checkout@v1 18 | 19 | - name: Installation 20 | run: | 21 | brew install cmake 22 | 23 | - name: Tests 24 | run: | 25 | ./scripts/test-unix.sh 26 | -------------------------------------------------------------------------------- /.github/workflows/windows.yml: -------------------------------------------------------------------------------- 1 | name: Windows 2 | 3 | on: 4 | pull_request: 5 | push: 6 | 7 | jobs: 8 | build: 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | os: [windows-latest] 13 | kind: [static, shared] 14 | 15 | runs-on: ${{ matrix.os }} 16 | 17 | steps: 18 | - uses: actions/checkout@v1 19 | 20 | - name: Tests 21 | run: | 22 | cmake -DXREPO_PACKAGE_VERBOSE=ON example 23 | cmake --build . 24 | 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | CMakeLists.txt.user 2 | CMakeCache.txt 3 | CMakeFiles 4 | CMakeScripts 5 | Testing 6 | Makefile 7 | cmake_install.cmake 8 | install_manifest.txt 9 | compile_commands.json 10 | CTestTestfile.cmake 11 | _deps 12 | cmake.log.* 13 | make.log.* 14 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | If you discover issues, have ideas for improvements or new features, or 4 | want to contribute a new module, please report them to the 5 | [issue tracker][1] of the repository or submit a pull request. Please, 6 | try to follow these guidelines when you do so. 7 | 8 | ## Issue reporting 9 | 10 | * Check that the issue has not already been reported. 11 | * Check that the issue has not already been fixed in the latest code 12 | (a.k.a. `master`). 13 | * Be clear, concise and precise in your description of the problem. 14 | * Open an issue with a descriptive title and a summary in grammatically correct, 15 | complete sentences. 16 | * Include any relevant code to the issue summary. 17 | 18 | ## Pull requests 19 | 20 | * Use a topic branch to easily amend a pull request later, if necessary. 21 | * Write good commit messages. 22 | * Use the same coding conventions as the rest of the project. 23 | * Ensure your edited codes with four spaces instead of TAB. 24 | * Please commit code to `dev` branch and we will merge into `master` branch in future. 25 | 26 | ### Some suggestions for developing code for this project 27 | 28 | #### Speed up build 29 | 30 | * Use `ccache`. 31 | * Pre-build by `make build -j`. Then if you do no modification on files in dir `core`, just use `scripts/get.sh __local__ __install_only__` to quickly install. 32 | * Use a real xmake executable file with environment variable `XMAKE_PROGRAM_DIR` set to dir `xmake` in repo path so that no installation is needed. 33 | 34 | #### Understand API layouts 35 | 36 | * Action scripts, plugin scripts and user's `xmake.lua` run in a sandbox. The sandbox API is in `xmake/core/sandbox`. 37 | * Utility scripts run in a base lua environment. Base API is in `xmake/core/base` 38 | * Native API, which includes the lua API and the xmake ext API, is written in C in `core/src/xmake` 39 | 40 | For example, to copy a directory in sandbox, the calling procedure is: `sandbox_os.cp()` -> `os.cp()` -> `xm_os_cpdir()` -> `tb_directory_copy()` 41 | 42 | ## Financial contributions 43 | 44 | We also welcome financial contributions in full transparency on our [sponsor](https://xmake.io/#/about/sponsor). 45 | Anyone can file an expense. If the expense makes sense for the development of the community, it will be "merged" in the ledger of our open collective by the core contributors and the person who filed the expense will be reimbursed. 46 | 47 | # 贡献代码 48 | 49 | 如果你发现一些问题,或者想新增或者改进某些新特性,或者想贡献一个新的模块 50 | 那么你可以在[issues][1]上提交反馈,或者发起一个提交代码的请求(pull request). 51 | 52 | ## 问题反馈 53 | 54 | * 确认这个问题没有被反馈过 55 | * 确认这个问题最近还没有被修复,请先检查下 `master` 的最新提交 56 | * 请清晰详细地描述你的问题 57 | * 如果发现某些代码存在问题,请在issue上引用相关代码 58 | 59 | ## 提交代码 60 | 61 | * 请先更新你的本地分支到最新,再进行提交代码请求,确保没有合并冲突 62 | * 编写友好可读的提交信息 63 | * 请使用与工程代码相同的代码规范 64 | * 确保提交的代码缩进是四个空格,而不是tab 65 | * 请提交代码到`dev`分支,如果通过,我们会在特定时间合并到`master`分支上 66 | * 为了规范化提交日志的格式,commit消息,不要用中文,请用英文描述 67 | 68 | [1]: https://github.com/xmake-io/xrepo-cmake/issues 69 | 70 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright 2022-present TBOOX Open Source Group 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | 204 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 | 3 | 4 | 5 | 6 |

xrepo-cmake

7 | 8 |
9 | 10 | github-ci 11 | 12 | 13 | github-ci 14 | 15 | 16 | github-ci 17 | 18 |
19 |
20 | 21 | license 22 | 23 | 24 | Reddit 25 | 26 | 27 | Gitter 28 | 29 | 30 | Telegram 31 | 32 | 33 | QQ 34 | 35 | 36 | Discord 37 | 38 | 39 | Donate 40 | 41 |
42 | 43 | CMake wrapper for Xrepo C and C++ package manager.
44 |
45 | 46 | ## Supporting the project 47 | 48 | Support this project by [becoming a sponsor](https://xmake.io/#/about/sponsor). Your logo will show up here with a link to your website. 🙏 49 | 50 | 51 | 52 | 53 | ## Introduction 54 | 55 | CMake wrapper for [Xrepo](https://xrepo.xmake.io/) C and C++ package manager. 56 | 57 | This allows using CMake to build your project, while using Xrepo to manage 58 | dependent packages. This project is partially inspired by 59 | [cmake-conan](https://github.com/conan-io/cmake-conan). 60 | 61 | Example use cases for this project: 62 | 63 | - Existing CMake projects which want to use Xrepo to manage packages. 64 | - New projects which have to use CMake, but want to use Xrepo to manage 65 | packages. 66 | 67 | Note: please use CMake 3.19 or later for reliable package usage in CMake code. 68 | 69 | ## Usage 70 | 71 | ### Apis 72 | 73 | #### xrepo_package 74 | 75 | [`xrepo.cmake`](./xrepo.cmake) provides `xrepo_package` function to manage 76 | packages. 77 | 78 | ```cmake 79 | xrepo_package( 80 | "foo 1.2.3" 81 | [CONFIGS feature1=true,feature2=false] 82 | [CONFIGS path/to/script.lua] 83 | [DEPS] 84 | [MODE debug|release] 85 | [ALIAS aliasname] 86 | [OUTPUT verbose|diagnosis|quiet] 87 | [DIRECTORY_SCOPE] 88 | ) 89 | ``` 90 | 91 | Some of the function arguments correspond directly to Xrepo command options. 92 | 93 | `xrepo_package` adds package install directory to `CMAKE_PREFIX_PATH`. So `find_package` 94 | can be used. If `CMAKE_MINIMUM_REQUIRED_VERSION` >= 3.1, cmake `PkgConfig` will also search 95 | for pkgconfig files under package install directories. 96 | 97 | After calling `xrepo_package(foo)`, there are three ways to use `foo` package: 98 | 99 | 1. Call `find_package(foo)` if package provides cmake config-files. 100 | - Refer to CMake [`find_package`](https://cmake.org/cmake/help/latest/command/find_package.html) documentation for more details. 101 | 2. If the package does not provide cmake config files or find modules 102 | - Following variables can be used to use the pacakge (variable names following cmake 103 | find modules [standard variable names](https://cmake.org/cmake/help/latest/manual/cmake-developer.7.html#standard-variable-names)) 104 | - `foo_INCLUDE_DIRS` 105 | - `foo_LIBRARY_DIRS` 106 | - `foo_LIBRARIES` 107 | - `foo_DEFINITIONS` 108 | - If `DIRECTORY_SCOPE` is specified, `xrepo_package` will run following code 109 | ```cmake 110 | include_directories(${foo_INCLUDE_DIRS}) 111 | link_directories(${foo_LIBRARY_DIRS}) 112 | ``` 113 | 3. Use `xrepo_target_packages`. Please refer to following section. 114 | 115 | Note `CONFIGS path/to/script.lua` is for fine control over package configs. 116 | For example: 117 | - Exclude packages on system. 118 | - Override dependent packages' default configs, e.g. set `shared=true`. 119 | 120 | If `DEPS` is specified, all dependent libraries will add to `CMAKE_PREFIX_PATH`, along with include, 121 | libraries being included in the four variables. 122 | 123 | #### xrepo_target_packages 124 | 125 | Add package includedirs and links/linkdirs to the given target. 126 | 127 | ```cmake 128 | xrepo_target_packages( 129 | target 130 | [NO_LINK_LIBRARIES] 131 | [PRIVATE|PUBLIC|INTERFACE] 132 | package1 package2 ... 133 | ) 134 | ``` 135 | 136 | - `NO_LINK_LIBRARIES` 137 | - In case a package provides multiple libs and user need to select which one 138 | to link, pass `NO_LINK_LIBRARIES` to disable calling `target_link_libraries`. 139 | User should call `target_link_libraries` to setup correct library linking. 140 | - `PRIVATE|PUBLIC|INTERFACE` 141 | - The default is not specifying propagation control keyword when calling 142 | `target_include_libraries`, `target_link_libraries`, etc, because there's no 143 | default choice on this in CMake. 144 | - Refer to this [Stack Overflow answer](https://stackoverflow.com/a/26038443) 145 | for differences. 146 | 147 | ### Use package from official repository 148 | 149 | Xrepo official repository: [xmake-repo](https://github.com/xmake-io/xmake-repo) 150 | 151 | Here's an example `CMakeLists.txt` that uses `gflags` package version 2.2.2 152 | managed by Xrepo. 153 | 154 | #### Integrate xrepo.cmake 155 | 156 | ```cmake 157 | cmake_minimum_required(VERSION 3.13.0) 158 | 159 | project(foo) 160 | 161 | # Download xrepo.cmake if not exists in build directory. 162 | if(NOT EXISTS "${CMAKE_BINARY_DIR}/xrepo.cmake") 163 | message(STATUS "Downloading xrepo.cmake from https://github.com/xmake-io/xrepo-cmake/") 164 | # mirror https://cdn.jsdelivr.net/gh/xmake-io/xrepo-cmake@main/xrepo.cmake 165 | file(DOWNLOAD "https://raw.githubusercontent.com/xmake-io/xrepo-cmake/main/xrepo.cmake" 166 | "${CMAKE_BINARY_DIR}/xrepo.cmake" 167 | TLS_VERIFY ON) 168 | endif() 169 | 170 | # Include xrepo.cmake so we can use xrepo_package function. 171 | include(${CMAKE_BINARY_DIR}/xrepo.cmake) 172 | ``` 173 | 174 | #### Add basic packages 175 | 176 | ```cmake 177 | xrepo_package("zlib") 178 | 179 | add_executable(example-bin "") 180 | target_sources(example-bin PRIVATE 181 | src/main.cpp 182 | ) 183 | xrepo_target_packages(example-bin zlib) 184 | ``` 185 | 186 | #### Add packages with configs 187 | 188 | ```cmake 189 | xrepo_package("gflags 2.2.2" CONFIGS "shared=true,mt=true") 190 | 191 | add_executable(example-bin "") 192 | target_sources(example-bin PRIVATE 193 | src/main.cpp 194 | ) 195 | xrepo_target_packages(example-bin gflags) 196 | ``` 197 | 198 | #### Add packages with cmake modules 199 | 200 | ```cmake 201 | xrepo_package("gflags 2.2.2" CONFIGS "shared=true,mt=true") 202 | 203 | # `xrepo_package` add gflags install directory to CMAKE_PREFIX_PATH. 204 | # As gflags provides cmake config-files, we can now call `find_package` to find 205 | # gflags package. 206 | find_package(gflags CONFIG COMPONENTS shared) 207 | 208 | add_executable(example-bin "") 209 | target_sources(example-bin PRIVATE 210 | src/main.cpp 211 | ) 212 | target_link_libraries(example-bin gflags) 213 | ``` 214 | 215 | #### Add custom packages 216 | 217 | We can also add custom packages in our project. 218 | 219 | ```cmake 220 | set(XREPO_XMAKEFILE ${CMAKE_CURRENT_SOURCE_DIR}/packages/xmake.lua) 221 | xrepo_package("myzlib") 222 | 223 | add_executable(example-bin "") 224 | target_sources(example-bin PRIVATE 225 | src/main.cpp 226 | ) 227 | xrepo_target_packages(example-bin myzlib) 228 | ``` 229 | 230 | Define myzlib package in packages/xmake.lua 231 | 232 | ```lua 233 | package("myzlib") 234 | set_homepage("http://www.zlib.net") 235 | set_description("A Massively Spiffy Yet Delicately Unobtrusive Compression Library") 236 | 237 | set_urls("http://zlib.net/zlib-$(version).tar.gz", 238 | "https://downloads.sourceforge.net/project/libpng/zlib/$(version)/zlib-$(version).tar.gz") 239 | 240 | add_versions("1.2.10", "8d7e9f698ce48787b6e1c67e6bff79e487303e66077e25cb9784ac8835978017") 241 | 242 | on_install(function (package) 243 | -- TODO 244 | end) 245 | 246 | on_test(function (package) 247 | assert(package:has_cfuncs("inflate", {includes = "zlib.h"})) 248 | end) 249 | ``` 250 | 251 | We can write a custom package in xmake.lua, please refer [Define Xrepo package](https://xmake.io/#/package/remote_package?id=package-description). 252 | 253 | ### Options and variables for `xrepo.cmake` 254 | 255 | Following options can be speicified with `cmake -D=`. 256 | Or use `set(var value)` in `CMakeLists.txt`. 257 | 258 | - `XMAKE_CMD`: string, defaults to empty string 259 | - Specify path to `xmake` command. Use this option if `xmake` is not installed 260 | in standard location and can't be detected automatically. 261 | - `XREPO_PACKAGE_VERBOSE`: `[ON|OFF]` 262 | - Enable verbose output for Xrepo Packages. 263 | - `XREPO_BOOTSTRAP_XMAKE`: `[ON|OFF]` 264 | - If `ON`, `xrepo.cmake` will install `xmake` if it is not found. 265 | - `XREPO_PACKAGE_DISABLE`: `[ON|OFF]` 266 | - Set this to `ON` to disable `xrepo_package` function. 267 | - If setting this variable in `CMakeLists.txt`, please set it before including 268 | `xrepo.cmake`. 269 | - `XREPO_BUILD_PARALLEL_JOBS`: `[COUNT]` 270 | - Set parallel compilation threads num function. 271 | - If setting this variable in `CMakeLists.txt`, please set it before including 272 | `xrepo.cmake`. 273 | - `XMAKE_RELEASE_LATEST`: `[VERSION]` 274 | - Set xmake version. 275 | - If setting this variable in `CMakeLists.txt`, please set it before including 276 | `xrepo.cmake`. 277 | 278 | ### Switching compiler and cross compilation 279 | 280 | Following variables controll cross compilation. Note: to specify a different compiler other than 281 | the default one on system, platform must be set to "cross". 282 | 283 | - `XREPO_TOOLCHAIN`: string, defaults to empty string 284 | - Specify toolchain name. Run `xmake show -l toolchains` to see available toolchains. 285 | - `XREPO_PLATFORM`: string, defaults to empty string 286 | - Specify platform name. If `XREPO_TOOLCHAIN` is specified and this is not, 287 | `XREPO_PLATFORM` will be set to `cross`. 288 | - `XREPO_ARCH`: string, defaults to empty string 289 | - Specify architecture name. 290 | - `XREPO_XMAKEFILE`: string, defaults to empty string 291 | - Specify Xmake script file of Xrepo package. 292 | 293 | ### Use package from 3rd repository 294 | 295 | In addition to installing packages from officially maintained repository, 296 | Xrepo can also install packages from third-party package managers such as vcpkg/conan/conda/pacman/homebrew/apt/dub/cargo. 297 | 298 | For the use of the command line, we can refer to the documentation: [Xrepo command usage](https://xrepo.xmake.io/#/getting_started?id=install-packages-from-third-party-package-manager) 299 | 300 | We can also use it directly in cmake to install packages from third-party repositories, just add the repository name as a namespace. e.g. `vcpkg::zlib`, `conan::pcre2` 301 | 302 | #### Conan 303 | 304 | ```cmake 305 | xrepo_package("conan::gflags/2.2.2") 306 | ``` 307 | 308 | #### Conda 309 | 310 | ```cmake 311 | xrepo_package("conda::gflags 2.2.2") 312 | ``` 313 | 314 | #### Vcpkg 315 | 316 | ```cmake 317 | xrepo_package("vcpkg::gflags") 318 | ``` 319 | 320 | #### Homebrew 321 | 322 | ```cmake 323 | xrepo_package("brew::gflags") 324 | ``` 325 | 326 | ## How does it work? 327 | 328 | [`xrepo.cmake`](./xrepo.cmake) module basically does the following tasks: 329 | 330 | - Call `xrepo install` to ensure specific package is installed. 331 | - Call `xrepo fetch` to get package information and setup various variables for 332 | using the installed package in CMake. 333 | 334 | The following section is a short introduction to using Xrepo. It helps to 335 | understand how `xrepo.cmake` works and how to specify some of the options in 336 | `xrepo_package`. 337 | 338 | ### Xrepo workflow 339 | 340 | Assmuing [Xmake](https://github.com/xmake-io/xmake/) is installed. 341 | 342 | Suppose we want to use `gflags` packages. 343 | 344 | First, search for `gflags` package in Xrepo. 345 | 346 | ``` 347 | $ xrepo search gflags 348 | The package names: 349 | gflags: 350 | -> gflags-v2.2.2: The gflags package contains a C++ library that implements commandline flags processing. (in builtin-repo) 351 | ``` 352 | 353 | It's already in Xrepo, so we can use it. If it's not in Xrepo, we can create it in 354 | [self-built repositories](https://xrepo.xmake.io/#/getting_started?id=suppory-distributed-repository). 355 | 356 | Let's see what configs are available for the package before using it: 357 | 358 | ``` 359 | $ xrepo info gflags 360 | ... 361 | -> configs: 362 | -> mt: Build the multi-threaded gflags library. (default: false) 363 | -> configs (builtin): 364 | -> debug: Enable debug symbols. (default: false) 365 | -> shared: Build shared library. (default: false) 366 | -> pic: Enable the position independent code. (default: true) 367 | ... 368 | ``` 369 | 370 | Suppose we want to use multi-threaded gflags shared library. We can install the package with following command: 371 | 372 | ``` 373 | xrepo install --mode=release --configs='mt=true,shared=true' 'gflags 2.2.2' 374 | ``` 375 | 376 | Only the first call to the above command will compile and install the package. 377 | To speed up cmake configuration, `xrepo` command will only be executed when the 378 | package is not installed or `xrepo_package` parameters have changed. 379 | 380 | After package installation, because we are using CMake instead of Xmake, we have 381 | to get package installation information by ourself. `xrepo fetch` command does 382 | exactly this: 383 | 384 | ``` 385 | xrepo fetch --json --mode=release --configs='mt=true,shared=true' 'gflags 2.2.2' 386 | ``` 387 | 388 | The above command will print out package's include, library directory along with 389 | other information. `xrepo_package` uses these information to setup variables to use 390 | the specified package. 391 | 392 | For CMake 3.19 and later which has JSON support, `xrepo_package` parses the JSON 393 | output. For previous version of CMake, `xrepo_package` uses only the `--cflags` option 394 | to get package include directory. Library and cmake module directory are infered from that 395 | directory, so it maybe unreliable to detect the correct paths. 396 | -------------------------------------------------------------------------------- /example/.gitignore: -------------------------------------------------------------------------------- 1 | # Xmake cache 2 | .xmake/ 3 | build/ 4 | 5 | # MacOS Cache 6 | .DS_Store 7 | 8 | 9 | -------------------------------------------------------------------------------- /example/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # project 2 | cmake_minimum_required(VERSION 3.13.0) 3 | project(example LANGUAGES C CXX ASM) 4 | 5 | # Download xrepo.cmake if not exists in build directory. 6 | if(NOT EXISTS "${CMAKE_BINARY_DIR}/xrepo.cmake") 7 | message(STATUS "Downloading xrepo.cmake from https://github.com/xmake-io/xrepo-cmake/") 8 | # mirror https://cdn.jsdelivr.net/gh/xmake-io/xrepo-cmake@main/xrepo.cmake 9 | file(DOWNLOAD "https://raw.githubusercontent.com/xmake-io/xrepo-cmake/main/xrepo.cmake" 10 | "${CMAKE_BINARY_DIR}/xrepo.cmake" 11 | TLS_VERIFY ON) 12 | endif() 13 | 14 | # Include xrepo.cmake so we can use xrepo_package function. 15 | include(${CMAKE_BINARY_DIR}/xrepo.cmake) 16 | 17 | # Call `xrepo_package` function to use default pcre2 18 | xrepo_package("pcre2" MODE debug) 19 | 20 | # Call `xrepo_package` function to use gflags 2.2.2 with specific configs. 21 | xrepo_package("gflags 2.2.2" 22 | CONFIGS "shared=true,mt=true") 23 | 24 | xrepo_package("glog" 25 | # xrepo_package("gflags") would be unnecessary if we use DEPS to include dependent libraries. 26 | #DEPS 27 | CONFIGS "packages/glog.lua") 28 | 29 | # find_pacakge works because package install dir is added to CMAKE_PREFIX_PATH. 30 | find_package(gflags) 31 | find_package(glog) 32 | 33 | # Call `xrepo_package` function to use custom package: myzlib 34 | set(XREPO_XMAKEFILE ${CMAKE_CURRENT_SOURCE_DIR}/packages/xmake.lua) 35 | xrepo_package("myzlib" ALIAS zlib) 36 | 37 | # Define target. We run cmake in example parent directory, thus we can't use 38 | # example as target name. 39 | add_executable(example-bin "") 40 | target_sources(example-bin PRIVATE 41 | src/main.cpp 42 | ) 43 | # For packges that does not provide cmake config-file packages, 44 | # xrepo_target_packges is convenient to setup include, library path and link targets. 45 | # We can specify multiple packages in a single call. 46 | #xrepo_target_packages(example-bin pcre2 zlib) 47 | 48 | # We can also specify visibility for dependent libraries. 49 | xrepo_target_packages(example-bin PRIVATE pcre2) 50 | if(WIN32) 51 | xrepo_target_packages(example-bin PUBLIC zlib) 52 | else() 53 | # NO_LINK_LIBRARIES does not call target_link_libraries. User can select which lib to link with. 54 | # Note different platforms would use different lib name, thus its easier to use 55 | # xrepo_target_packages when possible. 56 | xrepo_target_packages(example-bin PUBLIC zlib NO_LINK_LIBRARIES) 57 | target_link_libraries(example-bin PUBLIC z) 58 | endif() 59 | # For packages that provide config-file packages, we can still use imported 60 | # targets for linking. glog::glog has dependency on gflags, thus not needed 61 | # here. 62 | target_link_libraries(example-bin PRIVATE glog::glog) 63 | 64 | # This is for testing pkgconfig path settings. 65 | find_package(PkgConfig) 66 | pkg_check_modules(pcre2 libpcre2-posix) 67 | message(STATUS "pkg_check_modules pcre2_CFLAGS: ${pcre2_CFLAGS}") 68 | -------------------------------------------------------------------------------- /example/packages/common.lua: -------------------------------------------------------------------------------- 1 | function gflags_configs() 2 | return {system = false, configs = {mt = true, shared = true}} 3 | end 4 | -------------------------------------------------------------------------------- /example/packages/gflags.lua: -------------------------------------------------------------------------------- 1 | includes("common.lua") 2 | add_requires("gflags", gflags_configs()) 3 | -------------------------------------------------------------------------------- /example/packages/glog.lua: -------------------------------------------------------------------------------- 1 | includes("common.lua") 2 | add_requires("glog", {system = false, configs = {gflags = true, shared = true}}) 3 | add_requireconfs("glog.gflags", gflags_configs()) 4 | -------------------------------------------------------------------------------- /example/packages/xmake.lua: -------------------------------------------------------------------------------- 1 | package("myzlib") 2 | set_homepage("http://www.zlib.net") 3 | set_description("A Massively Spiffy Yet Delicately Unobtrusive Compression Library") 4 | 5 | add_urls("https://github.com/madler/zlib/archive/$(version).tar.gz", 6 | "https://github.com/madler/zlib.git") 7 | 8 | add_versions("v1.2.10", "42cd7b2bdaf1c4570e0877e61f2fdc0bce8019492431d054d3d86925e5058dc5") 9 | add_versions("v1.2.11", "629380c90a77b964d896ed37163f5c3a34f6e6d897311f1df2a7016355c45eff") 10 | 11 | on_install(function (package) 12 | io.writefile("xmake.lua", [[ 13 | includes("check_cincludes.lua") 14 | add_rules("mode.debug", "mode.release") 15 | target("zlib") 16 | set_kind("$(kind)") 17 | if not is_plat("windows", "mingw") then 18 | set_basename("z") 19 | end 20 | add_files("adler32.c") 21 | add_files("compress.c") 22 | add_files("crc32.c") 23 | add_files("deflate.c") 24 | add_files("gzclose.c") 25 | add_files("gzlib.c") 26 | add_files("gzread.c") 27 | add_files("gzwrite.c") 28 | add_files("inflate.c") 29 | add_files("infback.c") 30 | add_files("inftrees.c") 31 | add_files("inffast.c") 32 | add_files("trees.c") 33 | add_files("uncompr.c") 34 | add_files("zutil.c") 35 | add_headerfiles("zlib.h", "zconf.h") 36 | check_cincludes("Z_HAVE_UNISTD_H", "unistd.h") 37 | check_cincludes("HAVE_SYS_TYPES_H", "sys/types.h") 38 | check_cincludes("HAVE_STDINT_H", "stdint.h") 39 | check_cincludes("HAVE_STDDEF_H", "stddef.h") 40 | if is_plat("windows") then 41 | add_defines("_CRT_SECURE_NO_DEPRECATE") 42 | add_defines("_CRT_NONSTDC_NO_DEPRECATE") 43 | if is_kind("shared") then 44 | add_defines("ZLIB_DLL") 45 | end 46 | else 47 | add_defines("ZEXPORT=__attribute__((visibility(\"default\")))") 48 | add_defines("_LARGEFILE64_SOURCE=1") 49 | end 50 | ]]) 51 | local configs = {} 52 | if package:config("shared") then 53 | configs.kind = "shared" 54 | elseif not package:is_plat("windows", "mingw") and package:config("pic") ~= false then 55 | configs.cxflags = "-fPIC" 56 | end 57 | import("package.tools.xmake").install(package, configs) 58 | end) 59 | 60 | on_test(function (package) 61 | assert(package:has_cfuncs("inflate", {includes = "zlib.h"})) 62 | end) 63 | package_end() 64 | -------------------------------------------------------------------------------- /example/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, char** argv) 6 | { 7 | google::InitGoogleLogging(argv[0]); 8 | gflags::SetUsageMessage("xrepo-cmake example app."); 9 | gflags::ParseCommandLineFlags(&argc, &argv, true); 10 | 11 | FLAGS_logtostderr = 1; 12 | 13 | LOG(INFO) << "hello xrepo"; 14 | 15 | pcre2_general_context* gctx = pcre2_general_context_create(NULL, NULL, NULL); 16 | if (gctx) { 17 | pcre2_general_context_free(gctx); 18 | } 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /scripts/test-unix.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -x 4 | set -e 5 | 6 | rm -rf CMakeCache.txt CMakeFiles/ cmake_install.cmake 7 | 8 | # First build. 9 | output=cmake.log.0 10 | cmake -DXREPO_PACKAGE_VERBOSE=ON example | tee $output 11 | grep -E 'mode=debug pcre2' $output 12 | grep -E "pcre2_INCLUDE_DIRS" $output 13 | grep -E "pcre2_LIBRARY_DIRS" $output 14 | grep -E "pcre2_LINK_LIBRARIES" $output 15 | grep -E "pcre2_DEFINITIONS" $output 16 | grep -E "gflags prepend to CMAKE_PREFIX_PATH" $output 17 | grep -E "glog prepend to CMAKE_PREFIX_PATH" $output 18 | grep -E "zlib_INCLUDE_DIRS" $output 19 | grep -E "zlib prepend to CMAKE_PREFIX_PATH" $output 20 | grep -E "pkg_check_modules pcre2_CFLAGS.*packages/p/pcre2" $output 21 | grep -E "target_link_libraries\(example-bin PRIVATE pcre2-posix;pcre2-8\)" $output 22 | grep -v -E "xrepo: target_link_libraries\(example-bin PRIVATE z\)" $output 23 | make 24 | 25 | # Check for cached variables. 26 | output=make.log.1 27 | touch example/CMakeLists.txt 28 | make | tee $output 29 | 30 | match_cached_output="already installed, using cached variables" 31 | 32 | grep -E "pcre2 $match_cached_output" $output 33 | grep -E "gflags 2.2.2 $match_cached_output" $output 34 | grep -E "glog $match_cached_output" $output 35 | grep -E "myzlib $match_cached_output" $output 36 | 37 | # Check for update of config lua script. 38 | output=make.log.2 39 | touch example/packages/glog.lua 40 | make | tee $output 41 | grep -E "pcre2 $match_cached_output" $output 42 | grep -E "gflags 2.2.2 $match_cached_output" $output 43 | grep -E "example/packages/glog.lua" $output 44 | grep -E "myzlib $match_cached_output" $output 45 | -------------------------------------------------------------------------------- /xrepo.cmake: -------------------------------------------------------------------------------- 1 | option(XREPO_PACKAGE_DISABLE "Disable Xrepo Packages" OFF) 2 | option(XREPO_PACKAGE_VERBOSE "Enable verbose output for Xrepo Packages" OFF) 3 | option(XREPO_BOOTSTRAP_XMAKE "Bootstrap Xmake automatically" ON) 4 | 5 | # Following options are for cross compilation, or when specifying a specific compiler. 6 | set(XREPO_PLATFORM "" CACHE STRING "Xrepo package platform") 7 | set(XREPO_ARCH "" CACHE STRING "Xrepo package architecture") 8 | set(XREPO_TOOLCHAIN "" CACHE STRING "Xrepo package toolchain") 9 | set(XREPO_XMAKEFILE "" CACHE STRING "Xmake script file of Xrepo package") 10 | 11 | # xrepo_package: 12 | # 13 | # Parameters: 14 | # package_spec: required 15 | # The package name and version recognized by xrepo. 16 | # CONFIGS: optional 17 | # Run `xrepo info ` to see what configs are available. 18 | # There are two ways to specify configs: 19 | # 1. String, for example "shared=true,ssl=openssl" 20 | # 2. Path to a lua script. This is for fine control over package configs. 21 | # Refer to examples for how to use this. 22 | # Note: 23 | # - Do not use ~ to refer to home directory. Non-absolute path 24 | # will be treated as relative to the current CMakeLists.txt. 25 | # - Only CONFIGS specified lua script modification time is checked 26 | # to decide whether xrepo install can be skipped. If using "includes" 27 | # in lua script, this is not reliable. Please touch the CONFIGS lua 28 | # script manually to trigger run xrepo install in that case. 29 | # DEPS: optional 30 | # If specified, include all dependent libraries' settings in various 31 | # variables. Also add all dependent libraries' install dir to 32 | # CMAKE_PREFIX_PATH. 33 | # USE_ABSOLUTE_LIBS: optional 34 | # If specified, forces the linker to use absolute paths to library files rather than 35 | # relying on the -l flag which only specifies the library name. This can be particularly 36 | # useful when you need to ensure that the linker finds the correct library version, 37 | # especially in environments where multiple versions of the same library might be present. 38 | # MODE: optional, debug|release 39 | # Pass `--mode` option to xrepo install command. If not specified, 40 | # `--mode` option is not passed. 41 | # OUTPUT: optional, verbose|diagnosis|quiet 42 | # Control output for xrepo install command. 43 | # DIRECTORY_SCOPE: optional 44 | # If specified, setup include and link directories for the package in 45 | # CMake directory scope. CMake code in `add_subdirectory` can also use 46 | # the package directly. 47 | # 48 | # Example: 49 | # 50 | # xrepo_package( 51 | # "foo 1.2.3" 52 | # [CONFIGS feature1=true,feature2=false] 53 | # [CONFIGS path/to/script.lua] 54 | # [DEPS] 55 | # [USE_ABSOLUTE_LIBS] 56 | # [MODE debug|release] 57 | # [OUTPUT verbose|diagnosis|quiet] 58 | # [DIRECTORY_SCOPE] 59 | # ) 60 | # 61 | # `xrepo_package` does the following tasks for the above call: 62 | # 63 | # 1. Ensure specified package `foo` version 1.2.3 with given config is installed. 64 | # 2. Set variable `foo_INCLUDE_DIRS` and `foo_LIBRARY_DIRS` to header and library 65 | # path. 66 | # - Use these variables in `target_include_directories` and 67 | # `target_link_directories` to use the package. 68 | # - User should figure out what library to use for `target_link_libraries`. 69 | # - If `DIRECTORY_SCOPE` is specified, execute following code so the package 70 | # can be used in cmake's direcotry scope: 71 | # include_directories(foo_INCLUDE_DIRS) 72 | # link_directories(foo_LIBRARY_DIRS) 73 | # 3. Append package install directory to `CMAKE_PREFIX_PATH`. 74 | 75 | function(_install_xmake_program) 76 | if (NOT XMAKE_RELEASE_LATEST) 77 | set(XMAKE_RELEASE_LATEST 2.9.4) 78 | endif() 79 | set(XMAKE_VERSION master) 80 | set(XMAKE_BINARY_DIR ${CMAKE_BINARY_DIR}/xmake) 81 | message(STATUS "xmake not found, Install it to ${XMAKE_BINARY_DIR} automatically!") 82 | if(EXISTS "${XMAKE_BINARY_DIR}") 83 | file(REMOVE_RECURSE ${XMAKE_BINARY_DIR}) 84 | endif() 85 | 86 | # Download xmake archive file 87 | if(WIN32) 88 | set(XMAKE_ARCHIVE_FILE ${CMAKE_BINARY_DIR}/xmake-${XMAKE_VERSION}.win32.zip) 89 | set(XMAKE_ARCHIVE_URL https://github.com/xmake-io/xmake/releases/download/v${XMAKE_RELEASE_LATEST}/xmake-${XMAKE_VERSION}.win32.zip) 90 | else() 91 | set(XMAKE_ARCHIVE_FILE ${CMAKE_BINARY_DIR}/xmake-${XMAKE_VERSION}.tar.gz) 92 | set(XMAKE_ARCHIVE_URL https://github.com/xmake-io/xmake/releases/download/v${XMAKE_RELEASE_LATEST}/xmake-${XMAKE_VERSION}.tar.gz) 93 | endif() 94 | if(NOT EXISTS "${XMAKE_ARCHIVE_FILE}") 95 | message(STATUS "Downloading xmake from ${XMAKE_ARCHIVE_URL}") 96 | file(DOWNLOAD "${XMAKE_ARCHIVE_URL}" 97 | "${XMAKE_ARCHIVE_FILE}" 98 | TLS_VERIFY ON) 99 | endif() 100 | 101 | # Extract xmake archive file 102 | if(NOT EXISTS "${XMAKE_BINARY_DIR}") 103 | message(STATUS "Extracting ${XMAKE_ARCHIVE_FILE}") 104 | file(MAKE_DIRECTORY ${XMAKE_BINARY_DIR}) 105 | execute_process(COMMAND ${CMAKE_COMMAND} -E tar xzf ${XMAKE_ARCHIVE_FILE} 106 | WORKING_DIRECTORY ${XMAKE_BINARY_DIR} 107 | RESULT_VARIABLE exit_code) 108 | if(NOT "${exit_code}" STREQUAL "0") 109 | message(FATAL_ERROR "unzip ${XMAKE_ARCHIVE_FILE} failed, exit code: ${exit_code}") 110 | endif() 111 | endif() 112 | 113 | # Install xmake 114 | if(WIN32) 115 | set(XMAKE_BINARY ${XMAKE_BINARY_DIR}/xmake/xmake.exe) 116 | if(EXISTS ${XMAKE_BINARY}) 117 | set(XMAKE_CMD ${XMAKE_BINARY} PARENT_SCOPE) 118 | endif() 119 | else() 120 | set(XMAKE_SOURCE_DIR ${XMAKE_BINARY_DIR}/xmake-${XMAKE_RELEASE_LATEST}) 121 | message(STATUS "Configuring xmake") 122 | execute_process(COMMAND ${CMAKE_COMMAND} -E env --unset=CC --unset=CXX --unset=LD ./configure 123 | WORKING_DIRECTORY ${XMAKE_SOURCE_DIR} 124 | RESULT_VARIABLE exit_code) 125 | if(NOT "${exit_code}" STREQUAL "0") 126 | message(FATAL_ERROR "Configure xmake failed, exit code: ${exit_code}") 127 | endif() 128 | 129 | message(STATUS "Building xmake") 130 | execute_process(COMMAND ${CMAKE_COMMAND} -E env --unset=CC --unset=CXX --unset=LD make -j4 131 | WORKING_DIRECTORY ${XMAKE_SOURCE_DIR} 132 | RESULT_VARIABLE exit_code) 133 | if(NOT "${exit_code}" STREQUAL "0") 134 | message(FATAL_ERROR "Build xmake failed, exit code: ${exit_code}") 135 | endif() 136 | 137 | message(STATUS "Installing xmake") 138 | execute_process(COMMAND make install PREFIX=${XMAKE_BINARY_DIR}/install 139 | WORKING_DIRECTORY ${XMAKE_SOURCE_DIR} 140 | RESULT_VARIABLE exit_code) 141 | if(NOT "${exit_code}" STREQUAL "0") 142 | message(FATAL_ERROR "Install xmake failed, exit code: ${exit_code}") 143 | endif() 144 | 145 | set(XMAKE_BINARY ${XMAKE_BINARY_DIR}/install/bin/xmake) 146 | if(EXISTS ${XMAKE_BINARY}) 147 | set(XMAKE_CMD ${XMAKE_BINARY} PARENT_SCOPE) 148 | endif() 149 | endif() 150 | endfunction() 151 | 152 | macro(_detect_xmake_cmd) 153 | if(NOT XMAKE_CMD) 154 | # Note: if XMAKE_CMD is already defined, find_program does not search. 155 | # find_program makes XMAKE_CMD a cached variable. So find_program 156 | # searches only once for each cmake build directory. 157 | find_program(XMAKE_CMD xmake) 158 | endif() 159 | 160 | if(NOT XMAKE_CMD) 161 | if(WIN32) 162 | set(XMAKE_BINARY ${CMAKE_BINARY_DIR}/xmake/xmake/xmake.exe) 163 | else() 164 | set(XMAKE_BINARY ${CMAKE_BINARY_DIR}/xmake/install/bin/xmake) 165 | endif() 166 | if(EXISTS ${XMAKE_BINARY}) 167 | set(XMAKE_CMD ${XMAKE_BINARY}) 168 | endif() 169 | endif() 170 | if(NOT XMAKE_CMD AND XREPO_BOOTSTRAP_XMAKE) 171 | _install_xmake_program() 172 | endif() 173 | if(NOT XMAKE_CMD) 174 | message(FATAL_ERROR "xmake not found, Please install it first from https://xmake.io") 175 | endif() 176 | 177 | message(STATUS "xmake command: ${XMAKE_CMD}") 178 | set(XREPO_CMD ${XMAKE_CMD} lua private.xrepo) 179 | endmacro() 180 | 181 | function(_xrepo_detect_json_support) 182 | if(DEFINED XREPO_FETCH_JSON) 183 | return() 184 | endif() 185 | 186 | # Whether to use `xrepo fetch --json` to get package info. 187 | set(use_fetch_json ON) 188 | 189 | if(CMAKE_VERSION VERSION_LESS 3.19) 190 | message(WARNING "Please use CMake version >= 3.19 for JSON support. " 191 | "Otherwise xrepo_package maybe unreliable to setup package variables.") 192 | set(use_fetch_json OFF) 193 | elseif(XREPO_CMD) 194 | execute_process(COMMAND ${XREPO_CMD} fetch --help 195 | OUTPUT_VARIABLE help_output 196 | RESULT_VARIABLE exit_code) 197 | if(NOT "${exit_code}" STREQUAL "0") 198 | message(FATAL_ERROR "xrepo fetch --help failed, exit code: ${exit_code}") 199 | endif() 200 | 201 | if(NOT "${help_output}" MATCHES "--json") 202 | message(WARNING "xrepo fetch does not support --json (please upgrade xrepo/xmake to the latest version), " 203 | "xrepo_package maybe unreliable to setup package variables") 204 | set(use_fetch_json OFF) 205 | endif() 206 | endif() 207 | 208 | set(XREPO_FETCH_JSON ${use_fetch_json} CACHE BOOL "Use xrepo JSON output" FORCE) 209 | endfunction() 210 | 211 | function(_detect_toolchain) 212 | if(NOT "${XREPO_TOOLCHAIN}" STREQUAL "") 213 | return() 214 | endif() 215 | 216 | if(DEFINED CMAKE_C_COMPILER) 217 | get_filename_component(_compiler_name "${CMAKE_C_COMPILER}" NAME_WLE) 218 | elseif(DEFINED CMAKE_CXX_COMPILER) 219 | get_filename_component(_compiler_name "${CMAKE_CXX_COMPILER}" NAME_WLE) 220 | string(REPLACE "g++" "gcc" "${_compiler_name}" _compiler_name) 221 | string(REPLACE "clang++" "clang" "${_compiler_name}" _compiler_name) 222 | else() 223 | # Shouldn't reach here because cmake will try to detect compiler and set 224 | # corresponding variables. 225 | return() 226 | endif() 227 | 228 | if(("${_compiler_name}" MATCHES "^gcc") 229 | OR ("${_compiler_name}" MATCHES "^clang")) 230 | message(STATUS "xrepo: set(XREPO_TOOLCHAIN ${_compiler_name}) because CMAKE_C_COMPILER or CMAKE_CXX_COMPILER is set") 231 | set(XREPO_TOOLCHAIN "${_compiler_name}" PARENT_SCOPE) 232 | else() 233 | message(STATUS "xrepo: CMAKE_C_COMPILER=${CMAKE_C_COMPILER} CMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} using system default toolchain.") 234 | endif() 235 | endfunction() 236 | 237 | if(NOT XREPO_PACKAGE_DISABLE) 238 | # Setup for xmake. 239 | _detect_xmake_cmd() 240 | 241 | # Some cmake find module code may use pkgconfig to find header, library, etc. 242 | # Refer to https://cmake.org/cmake/help/latest/manual/cmake-developer.7.html#a-sample-find-module 243 | # If CMAKE_MINIMUM_REQUIRED_VERSION is 3.1 or later, paths in CMAKE_PREFIX_PATH are added to pkg-config 244 | # search path. (https://cmake.org/cmake/help/latest/module/FindPkgConfig.html#variable:PKG_CONFIG_USE_CMAKE_PREFIX_PATH) 245 | if(CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 3.1) 246 | message(WARNING "xrepo: CMAKE_MINIMUM_REQUIRED_VERSION less than 3.1. " 247 | "CMAKE_PREFIX_PATH are not included in pkg-config search. " 248 | "Some find module code may fail or resolve to system installed libraries.") 249 | endif() 250 | 251 | _xrepo_detect_json_support() 252 | message(STATUS "xrepo: fetch --json: ${XREPO_FETCH_JSON}") 253 | _detect_toolchain() 254 | if((NOT "${XREPO_TOOLCHAIN}" STREQUAL "") AND ("${XREPO_PLATFORM}" STREQUAL "")) 255 | message(STATUS "xrepo: XREPO_TOOLCHAIN is set but XREPO_PLATFORM is not, this is experimental feature of xmake") 256 | endif() 257 | endif() 258 | 259 | function(xrepo_package package) 260 | if(XREPO_PACKAGE_DISABLE) 261 | return() 262 | endif() 263 | 264 | set(options "DIRECTORY_SCOPE;DEPS;USE_ABSOLUTE_LIBS") 265 | set(one_value_args CONFIGS MODE OUTPUT ALIAS) 266 | cmake_parse_arguments(ARG "${options}" "${one_value_args}" "" ${ARGN}) 267 | 268 | # Construct options to xrepo install and fetch command. 269 | if(DEFINED ARG_CONFIGS) 270 | if(ARG_CONFIGS MATCHES "\\.lua$") 271 | if(IS_ABSOLUTE "${ARG_CONFIGS}" AND EXISTS "${ARG_CONFIGS}") 272 | set(_config_lua_script "${ARG_CONFIGS}") 273 | elseif(EXISTS "${CMAKE_CURRENT_LIST_DIR}/${ARG_CONFIGS}") 274 | set(_config_lua_script "${CMAKE_CURRENT_LIST_DIR}/${ARG_CONFIGS}") 275 | else() 276 | message(WARNING "CONFIGS ${ARG_CONFIGS} ends with '.lua' but no file found, taken as normal config") 277 | endif() 278 | endif() 279 | 280 | if(DEFINED _config_lua_script) 281 | set(configs "${_config_lua_script}") 282 | # Trigger cmake configure when ${_config_lua_script} changes. 283 | set_property( 284 | DIRECTORY 285 | APPEND 286 | PROPERTY CMAKE_CONFIGURE_DEPENDS 287 | "${_config_lua_script}" 288 | ) 289 | else() 290 | set(configs "--configs=${ARG_CONFIGS}") 291 | endif() 292 | endif() 293 | 294 | if(DEFINED ARG_MODE) 295 | set(mode "--mode=${ARG_MODE}") 296 | endif() 297 | 298 | if(XREPO_PACKAGE_VERBOSE) 299 | set(verbose "-vD") 300 | elseif(DEFINED ARG_OUTPUT) 301 | string(TOLOWER "${ARG_OUTPUT}" _output) 302 | if(_output STREQUAL "diagnosis") 303 | set(verbose "-vD") 304 | elseif(_output STREQUAL "verbose") 305 | set(verbose "-v") 306 | elseif(_output STREQUAL "quiet") 307 | set(verbose "-q") 308 | endif() 309 | endif() 310 | 311 | # Options for cross compilation and toolchain. 312 | if(NOT "${XREPO_PLATFORM}" STREQUAL "") 313 | set(platform "--plat=${XREPO_PLATFORM}") 314 | endif() 315 | if(NOT "${XREPO_ARCH}" STREQUAL "") 316 | set(arch "--arch=${XREPO_ARCH}") 317 | endif() 318 | if(NOT "${XREPO_TOOLCHAIN}" STREQUAL "") 319 | set(toolchain "--toolchain=${XREPO_TOOLCHAIN}") 320 | endif() 321 | if(NOT "${XREPO_XMAKEFILE}" STREQUAL "") 322 | set(includes "--includes=${XREPO_XMAKEFILE}") 323 | endif() 324 | 325 | # Get package_name that will be used as various variables' prefix. 326 | if(DEFINED ARG_ALIAS) 327 | _xrepo_package_name(${ARG_ALIAS}) 328 | else() 329 | _xrepo_package_name(${package}) 330 | endif() 331 | 332 | # Verbose option should not be passed to xrepo fetch. 333 | # Otherwise, the output would be invalid to parse. 334 | set(_xrepo_cmdargs ${platform} ${arch} ${toolchain} ${includes} ${mode} ${configs}) 335 | if(NOT DEFINED _config_lua_script) 336 | list(APPEND _xrepo_cmdargs ${package}) 337 | endif() 338 | 339 | # To speedup cmake re-configure, if xrepo command and args are the same as 340 | # cached value, load related variables from cache to avoid executing xrepo 341 | # command again. 342 | string(REGEX REPLACE ";" " " _xrepo_cmdargs_${package_name} "${XREPO_CMD} install ${_xrepo_cmdargs}") 343 | if(DEFINED _config_lua_script) 344 | file(TIMESTAMP ${_config_lua_script} _config_lua_script_modify_timestamp) 345 | set(_xrepo_cmdargs_${package_name} "${_xrepo_cmdargs_${package_name}} (mtime: ${_config_lua_script_modify_timestamp})") 346 | endif() 347 | 348 | if("${_cache_xrepo_cmdargs_${package_name}}" STREQUAL "${_xrepo_cmdargs_${package_name}}") 349 | message(STATUS "xrepo: ${package} already installed, using cached variables") 350 | 351 | foreach(var ${_cache_xrepo_vars_${package_name}}) 352 | message(STATUS "xrepo: ${var} ${${var}}") 353 | endforeach() 354 | 355 | _xrepo_finish_package_setup(${package_name}) 356 | return() 357 | endif() 358 | 359 | if(XREPO_BUILD_PARALLEL_JOBS) 360 | set(XREPO_BUILD_PARALLEL_JOBS_STR -j${XREPO_BUILD_PARALLEL_JOBS}) 361 | endif() 362 | 363 | message(STATUS "xrepo: ${_xrepo_cmdargs_${package_name}}") 364 | execute_process(COMMAND ${CMAKE_COMMAND} -E env --unset=CC --unset=CXX --unset=LD ${XREPO_CMD} install --yes ${verbose} ${XREPO_BUILD_PARALLEL_JOBS_STR} ${_xrepo_cmdargs} 365 | RESULT_VARIABLE exit_code) 366 | if(NOT "${exit_code}" STREQUAL "0") 367 | message(FATAL_ERROR "xrepo install ${package} failed, exit code: ${exit_code}") 368 | endif() 369 | 370 | if(XREPO_FETCH_JSON) 371 | _xrepo_fetch_json() 372 | else() 373 | _xrepo_fetch_cflags() 374 | endif() 375 | 376 | _xrepo_finish_package_setup(${package_name}) 377 | 378 | # Store xrepo command and arguments for furture comparison. 379 | set(_cache_xrepo_cmdargs_${package_name} "${_xrepo_cmdargs_${package_name}}" CACHE INTERNAL "") 380 | endfunction() 381 | 382 | function(xrepo_target_packages target) 383 | if(XREPO_PACKAGE_DISABLE) 384 | return() 385 | endif() 386 | 387 | set(options NO_LINK_LIBRARIES PRIVATE PUBLIC INTERFACE) 388 | cmake_parse_arguments(ARG "${options}" "" "" ${ARGN}) 389 | 390 | if(ARG_PRIVATE) 391 | set(_visibility "PRIVATE") 392 | elseif(ARG_PUBLIC) 393 | set(_visibility "PUBLIC") 394 | elseif(ARG_INTERFACE) 395 | set(_visibility "INTERFACE") 396 | else() 397 | set(_visibility "PRIVATE") 398 | endif() 399 | 400 | foreach(package_name IN LISTS ARG_UNPARSED_ARGUMENTS) 401 | if(DEFINED ${package_name}_INCLUDE_DIRS) 402 | message(STATUS "xrepo: target_include_directories(${target} ${_visibility} ${${package_name}_INCLUDE_DIRS})") 403 | target_include_directories(${target} ${_visibility} ${${package_name}_INCLUDE_DIRS}) 404 | endif() 405 | if(DEFINED ${package_name}_LIBRARY_DIRS) 406 | message(STATUS "xrepo: target_link_directories(${target} ${_visibility} ${${package_name}_LIBRARY_DIRS})") 407 | target_link_directories(${target} ${_visibility} ${${package_name}_LIBRARY_DIRS}) 408 | endif() 409 | if((DEFINED ${package_name}_LINK_LIBRARIES) AND (NOT ARG_NO_LINK_LIBRARIES)) 410 | message(STATUS "xrepo: target_link_libraries(${target} ${_visibility} ${${package_name}_LINK_LIBRARIES})") 411 | target_link_libraries(${target} ${_visibility} ${${package_name}_LINK_LIBRARIES}) 412 | endif() 413 | if((DEFINED ${package_name}_SYS_LIBRARIES) AND (NOT ARG_NO_LINK_LIBRARIES)) 414 | message(STATUS "xrepo: target_link_libraries(${target} ${_visibility} ${${package_name}_SYS_LIBRARIES})") 415 | target_link_libraries(${target} ${_visibility} ${${package_name}_SYS_LIBRARIES}) 416 | endif() 417 | if(DEFINED ${package_name}_DEFINITIONS) 418 | message(STATUS "xrepo: target_compile_definitions(${target} ${_visibility} ${${package_name}_DEFINITIONS})") 419 | target_compile_definitions(${target} ${_visibility} ${${package_name}_DEFINITIONS}) 420 | endif() 421 | endforeach() 422 | endfunction() 423 | 424 | # Append parent directorie of include directory to CMAKE_PREFIX_PATH. 425 | macro(_xrepo_set_cmake_prefix_path package_name) 426 | # CMake looks for quite a few directories under each prefix directory for config-file.cmake. 427 | # Thus Using CMAKE_PREFIX_PATH is easier and more reliable for config-file packages to be found 428 | # than setting _DIR. 429 | # Refer to https://cmake.org/cmake/help/latest/command/find_package.html#config-mode-search-procedure 430 | 431 | if(NOT DEFINED ${package_name}_INCLUDE_DIRS) 432 | return() 433 | endif() 434 | 435 | foreach(var ${${package_name}_INCLUDE_DIRS}) 436 | get_filename_component(_install_dir "${var}" DIRECTORY) 437 | if(NOT "${var}" IN_LIST CMAKE_PREFIX_PATH) 438 | # Use prepend to make xrepo packages have high priority in search. 439 | list(PREPEND CMAKE_PREFIX_PATH "${_install_dir}") 440 | message(STATUS "xrepo: ${package_name} prepend to CMAKE_PREFIX_PATH: ${_install_dir}") 441 | endif() 442 | unset(_install_dir) 443 | endforeach() 444 | endmacro() 445 | 446 | function(_xrepo_directory_scope package_name) 447 | if(DEFINED ${package_name}_INCLUDE_DIRS) 448 | message(STATUS "xrepo: directory scope include_directories(${${package_name}_INCLUDE_DIRS})") 449 | include_directories(${${package_name}_INCLUDE_DIRS}) 450 | endif() 451 | if(DEFINED ${package_name}_LIBRARY_DIRS) 452 | message(STATUS "xrepo: directory scope link_directories(${${package_name}_LIBRARY_DIRS})") 453 | link_directories(${${package_name}_LIBRARY_DIRS}) 454 | endif() 455 | endfunction() 456 | 457 | macro(_xrepo_finish_package_setup package_name) 458 | _xrepo_set_cmake_prefix_path(${package_name}) 459 | if(ARG_DIRECTORY_SCOPE) 460 | _xrepo_directory_scope(${package_name}) 461 | endif() 462 | 463 | set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} PARENT_SCOPE) 464 | endmacro() 465 | 466 | function(_xrepo_package_name package) 467 | # For find_package(pkg) to work, we need to set variable _DIR to the 468 | # cmake module directory provided by the package. Thus we need to extract 469 | # package name from package specification, which may also contain package 470 | # source and version. 471 | if(package MATCHES "^conan::.*") 472 | string(REGEX REPLACE "conan::([^/]+).*" "\\1" package_name ${package}) 473 | elseif(package MATCHES "^conda::.*") 474 | string(REGEX REPLACE "conda::([^ ]+).*" "\\1" package_name ${package}) 475 | elseif(package MATCHES "^vcpkg::.*") 476 | string(REGEX REPLACE "vcpkg::(.*)" "\\1" package_name ${package}) 477 | elseif(package MATCHES "^brew::.*") 478 | string(REGEX REPLACE "brew::([^/]+).*" "\\1" package_name ${package}) 479 | else() 480 | string(REGEX REPLACE "([^ ]+).*" "\\1" package_name ${package}) 481 | endif() 482 | 483 | set(package_name ${package_name} PARENT_SCOPE) 484 | endfunction() 485 | 486 | macro(_xrepo_fetch_json) 487 | if(ARG_DEPS) 488 | set(_xrepo_deps "--deps") 489 | endif() 490 | 491 | execute_process(COMMAND ${XREPO_CMD} fetch ${_xrepo_deps} --json ${_xrepo_cmdargs} 492 | OUTPUT_VARIABLE json_output 493 | ERROR_VARIABLE json_error_output 494 | RESULT_VARIABLE exit_code) 495 | if(NOT "${exit_code}" STREQUAL "0") 496 | message(STATUS "xrepo fetch --json:") 497 | message(STATUS "STDOUT:\n${json_output}") 498 | message(STATUS "STDERR:\n${json_error_output}") 499 | message(FATAL_ERROR "xrepo fetch --json failed, exit code: ${exit_code}") 500 | endif() 501 | 502 | # Loop over out most array for the json object. 503 | # The following code supports parsing the output of `xrepo fetch --deps`. 504 | # But pulling in the output of `--deps` is problematic because the dependent 505 | # libraries maybe using different configs. 506 | # For example, glog depends on gflags. But the gflags library pulled in by glog is with 507 | # default configs {mt=false,shared=false}, while the user maybe requiring gflags with 508 | # configs {mt=true,shared=true}. 509 | # It's error-prone so we don't support it for now. 510 | #message(STATUS "xrepo DEBUG: json output: ${json_output}") 511 | string(JSON len ERROR_VARIABLE json_error LENGTH ${json_output}) 512 | if(NOT "${json_error}" STREQUAL "NOTFOUND") 513 | message(STATUS "xrepo fetch --json:") 514 | message(STATUS "STDOUT:\n${json_output}") 515 | message(STATUS "location:\n${len}") 516 | message(FATAL_ERROR "xrepo fetch --json: fail to parse output, error: ${json_error}") 517 | endif() 518 | math(EXPR len_end "${len} - 1") 519 | foreach(idx RANGE 0 ${len_end}) 520 | # Loop over includedirs. 521 | string(JSON includedirs_type ERROR_VARIABLE includedirs_error TYPE ${json_output} ${idx} "includedirs") 522 | if("${includedirs_type}" STREQUAL "STRING") 523 | string(JSON dir GET ${json_output} ${idx} "includedirs") 524 | list(APPEND includedirs ${dir}) 525 | elseif("${includedirs_type}" MATCHES "ARRAY|OBJECT") 526 | string(JSON includedirs_len ERROR_VARIABLE includedirs_error LENGTH ${json_output} ${idx} "includedirs") 527 | if("${includedirs_error}" STREQUAL "NOTFOUND" AND NOT "${includedirs_len}" EQUAL 0) 528 | math(EXPR includedirs_end "${includedirs_len} - 1") 529 | foreach(includedirs_idx RANGE 0 ${includedirs_end}) 530 | string(JSON dir GET ${json_output} ${idx} "includedirs" ${includedirs_idx}) 531 | # It's difficult to know package name while looping over all packages. 532 | # Thus we use list to collect all include and link dirs. 533 | list(APPEND includedirs ${dir}) 534 | #message(STATUS "xrepo DEBUG: includedirs ${idx} ${includedirs_idx} ${dir}") 535 | endforeach() 536 | endif() 537 | endif() 538 | 539 | if (NOT ARG_USE_ABSOLUTE_LIBS) 540 | set (links_tag "links") 541 | # Loop over linkdirs. 542 | string(JSON linkdirs_type ERROR_VARIABLE linkdirs_error TYPE ${json_output} ${idx} "linkdirs") 543 | if("${linkdirs_type}" STREQUAL "STRING") 544 | string(JSON dir GET ${json_output} ${idx} "linkdirs") 545 | list(APPEND linkdirs ${dir}) 546 | elseif("${linkdirs_type}" MATCHES "ARRAY|OBJECT") 547 | string(JSON linkdirs_len ERROR_VARIABLE linkdirs_error LENGTH ${json_output} ${idx} "linkdirs") 548 | if("${linkdirs_error}" STREQUAL "NOTFOUND" AND NOT "${linkdirs_len}" EQUAL 0) 549 | math(EXPR linkdirs_end "${linkdirs_len} - 1") 550 | foreach(linkdirs_idx RANGE 0 ${linkdirs_end}) 551 | string(JSON dir GET ${json_output} ${idx} "linkdirs" ${linkdirs_idx}) 552 | list(APPEND linkdirs ${dir}) 553 | #message(STATUS "xrepo DEBUG: linkdirs ${idx} ${linkdirs_idx} ${dir}") 554 | endforeach() 555 | endif() 556 | endif() 557 | else() 558 | set (links_tag "libfiles") 559 | endif() 560 | 561 | # Loop over links. 562 | string(JSON links_type ERROR_VARIABLE links_error TYPE ${json_output} ${idx} ${links_tag}) 563 | if("${links_type}" STREQUAL "STRING") 564 | string(JSON libfile GET ${json_output} ${idx} ${links_tag}) 565 | list(APPEND links ${libfile}) 566 | elseif("${links_type}" MATCHES "ARRAY|OBJECT") 567 | string(JSON links_len ERROR_VARIABLE links_error LENGTH ${json_output} ${idx} ${links_tag}) 568 | if("${links_error}" STREQUAL "NOTFOUND" AND NOT "${links_len}" EQUAL 0) 569 | math(EXPR links_end "${links_len} - 1") 570 | foreach(links_idx RANGE 0 ${links_end}) 571 | string(JSON libfile GET ${json_output} ${idx} ${links_tag} ${links_idx}) 572 | list(APPEND links ${libfile}) 573 | #message(STATUS "xrepo DEBUG: links ${idx} ${links_idx} ${libfile}") 574 | endforeach() 575 | endif() 576 | endif() 577 | 578 | # Loop over syslinks. 579 | string(JSON syslinks_type ERROR_VARIABLE syslinks_error TYPE ${json_output} ${idx} "syslinks") 580 | if("${syslinks_type}" STREQUAL "STRING") 581 | string(JSON dir GET ${json_output} ${idx} "syslinks") 582 | list(APPEND syslinks ${dir}) 583 | elseif("${syslinks_type}" MATCHES "ARRAY|OBJECT") 584 | string(JSON syslinks_len ERROR_VARIABLE syslinks_error LENGTH ${json_output} ${idx} "syslinks") 585 | if("${syslinks_error}" STREQUAL "NOTFOUND" AND NOT "${syslinks_len}" EQUAL 0) 586 | math(EXPR syslinks_end "${syslinks_len} - 1") 587 | foreach(syslinks_idx RANGE 0 ${syslinks_end}) 588 | string(JSON dir GET ${json_output} ${idx} "syslinks" ${syslinks_idx}) 589 | list(APPEND syslinks ${dir}) 590 | #message(STATUS "xrepo DEBUG: syslinks ${idx} ${syslinks_idx} ${dir}") 591 | endforeach() 592 | endif() 593 | endif() 594 | 595 | # Loop over defines. 596 | string(JSON defines_type ERROR_VARIABLE defines_error TYPE ${json_output} ${idx} "defines") 597 | if("${defines_type}" STREQUAL "STRING") 598 | string(JSON dir GET ${json_output} ${idx} "defines") 599 | list(APPEND defines ${dir}) 600 | elseif("${defines_type}" MATCHES "ARRAY|OBJECT") 601 | string(JSON defines_len ERROR_VARIABLE defines_error LENGTH ${json_output} ${idx} "defines") 602 | if("${defines_error}" STREQUAL "NOTFOUND" AND NOT "${defines_len}" EQUAL 0) 603 | math(EXPR defines_end "${defines_len} - 1") 604 | foreach(defines_idx RANGE 0 ${defines_end}) 605 | string(JSON dir GET ${json_output} ${idx} "defines" ${defines_idx}) 606 | list(APPEND defines ${dir}) 607 | #message(STATUS "xrepo DEBUG: defines ${idx} ${defines_idx} ${dir}") 608 | endforeach() 609 | endif() 610 | endif() 611 | endforeach() 612 | 613 | if(DEFINED includedirs) 614 | set(${package_name}_INCLUDE_DIRS "${includedirs}" CACHE INTERNAL "") 615 | set(${package_name}_INCLUDE_DIR "${includedirs}" CACHE INTERNAL "") # Keep compatibility, remove in the future. 616 | list(APPEND xrepo_vars_${package_name} ${package_name}_INCLUDE_DIRS) 617 | message(STATUS "xrepo: ${package_name}_INCLUDE_DIRS ${${package_name}_INCLUDE_DIRS}") 618 | else() 619 | message(STATUS "xrepo fetch --json: ${package_name} includedirs not found") 620 | endif() 621 | 622 | if(DEFINED linkdirs) 623 | set(${package_name}_LIBRARY_DIRS "${linkdirs}" CACHE INTERNAL "") 624 | set(${package_name}_LINK_DIR "${includedirs}" CACHE INTERNAL "") # Keep compatibility, remove in the future. 625 | list(APPEND xrepo_vars_${package_name} ${package_name}_LIBRARY_DIRS) 626 | message(STATUS "xrepo: ${package_name}_LIBRARY_DIRS ${${package_name}_LIBRARY_DIRS}") 627 | else() 628 | message(STATUS "xrepo fetch --json: ${package_name} linkdirs not found") 629 | endif() 630 | 631 | if(DEFINED links) 632 | set(${package_name}_LINK_LIBRARIES "${links}" CACHE INTERNAL "") 633 | set(${package_name}_LIBRARIES "${links}" CACHE INTERNAL "") 634 | list(APPEND xrepo_vars_${package_name} ${package_name}_LINK_LIBRARIES) 635 | message(STATUS "xrepo: ${package_name}_LINK_LIBRARIES ${${package_name}_LINK_LIBRARIES}") 636 | else() 637 | message(STATUS "xrepo fetch --json: ${package_name} links not found") 638 | endif() 639 | 640 | if(DEFINED syslinks) 641 | set(${package_name}_SYS_LIBRARIES "${syslinks}" CACHE INTERNAL "") 642 | list(APPEND xrepo_vars_${package_name} ${package_name}_SYS_LIBRARIES) 643 | message(STATUS "xrepo: ${package_name}_SYS_LIBRARIES ${${package_name}_SYS_LIBRARIES}") 644 | endif() 645 | 646 | if(DEFINED defines) 647 | set(${package_name}_DEFINITIONS "${defines}" CACHE INTERNAL "") 648 | list(APPEND xrepo_vars_${package_name} ${package_name}_DEFINITIONS) 649 | message(STATUS "xrepo: ${package_name}_DEFINITIONS ${${package_name}_DEFINITIONS}") 650 | endif() 651 | 652 | set(_cache_xrepo_vars_${package_name} "${xrepo_vars_${package_name}}" CACHE INTERNAL "") 653 | endmacro() 654 | 655 | macro(_xrepo_fetch_cflags) 656 | # Use cflags to get include path. Then we look for lib and cmake dir relative to include path. 657 | execute_process(COMMAND ${XREPO_CMD} fetch --cflags ${_xrepo_cmdargs} 658 | OUTPUT_VARIABLE cflags_output 659 | ERROR_VARIABLE cflags_error_output 660 | RESULT_VARIABLE exit_code) 661 | if(NOT "${exit_code}" STREQUAL "0") 662 | message(STATUS "xrepo fetch --cflags:") 663 | message(STATUS "STDOUT:\n${cflags_output}") 664 | message(STATUS "STDERR:\n${cflags_error_output}") 665 | message(FATAL_ERROR "xrepo fetch --cflags failed, exit code: ${exit_code}") 666 | endif() 667 | 668 | string(REGEX REPLACE "-I(.*)/include.*" "\\1" install_dir ${cflags_output}) 669 | 670 | set(${package_name}_INCLUDE_DIRS "${install_dir}/include" CACHE INTERNAL "") 671 | list(APPEND xrepo_vars_${package_name} ${package_name}_INCLUDE_DIRS) 672 | message(STATUS "xrepo: ${package_name}_INCLUDE_DIRS ${${package_name}_INCLUDE_DIRS}") 673 | 674 | if(EXISTS "${install_dir}/lib") 675 | set(${package_name}_LIBRARY_DIRS "${install_dir}/lib" CACHE INTERNAL "") 676 | list(APPEND xrepo_vars_${package_name} ${package_name}_LIBRARY_DIRS) 677 | message(STATUS "xrepo: ${package_name}_LIBRARY_DIRS ${${package_name}_LIBRARY_DIRS}") 678 | endif() 679 | if(EXISTS "${install_dir}/lib/cmake/${package_name}") 680 | set(${package_name}_DIR "${install_dir}/lib/cmake/${package_name}" CACHE INTERNAL "") 681 | list(APPEND xrepo_vars_${package_name} ${package_name}_DIR) 682 | message(STATUS "xrepo: ${package_name}_DIR ${${package_name}_DIR}") 683 | endif() 684 | 685 | set(_cache_xrepo_vars_${package_name} "${xrepo_vars_${package_name}}" CACHE INTERNAL "") 686 | endmacro() 687 | --------------------------------------------------------------------------------