hidecmakelinker
所属分类:开发工具
开发工具:Nim
文件大小:0KB
下载次数:0
上传日期:2023-10-17 08:37:52
上 传 者:
sh-1993
说明: 使用Nim编程语言中的CMake来使用C C++库的工具和库,
(Tool and library to use C C++ libraries using CMake from Nim programming language,)
文件列表:
LICENSE (1065, 2023-11-13)
hidecmakelinker.nimble (527, 2023-11-13)
src/ (0, 2023-11-13)
src/hidecmakelinker.nim (5140, 2023-11-13)
src/hidecmakelinkerpkg/ (0, 2023-11-13)
src/hidecmakelinkerpkg/libconf.nim (4507, 2023-11-13)
src/hidecmakelinkerpkg/libparams.nim (2508, 2023-11-13)
src/void.nim (0, 2023-11-13)
tests/ (0, 2023-11-13)
tests/clib/ (0, 2023-11-13)
tests/clib/libtestc.c (35, 2023-11-13)
tests/clib/libtestc.cmake (228, 2023-11-13)
tests/clib/libtestc.h (99, 2023-11-13)
tests/config.nims (452, 2023-11-13)
tests/cpplib/ (0, 2023-11-13)
tests/cpplib/libtestcpp.cmake (246, 2023-11-13)
tests/cpplib/libtestcpp.cpp (142, 2023-11-13)
tests/cpplib/libtestcpp.hpp (148, 2023-11-13)
tests/testmodules/ (0, 2023-11-13)
tests/testmodules/libtestc.nim (489, 2023-11-13)
tests/testmodules/libtestcpp.nim (785, 2023-11-13)
tests/testmodules/neolib.nim (38, 2023-11-13)
tests/testmodules/testcmakecmd.nim (445, 2023-11-13)
tests/testprojc.nim (260, 2023-11-13)
tests/testprojcpp.nim (236, 2023-11-13)
# hidecmakelinker
hidecmakelinker is a tool to use C/C++ libraries using CMake from [Nim programming language](https://nim-lang.org).
It was created to use Raspberry Pi Pico SDK from Nim but hidecmakelinker is written so that it can be used for other C/C++ projects using CMake and it doesn't contains any Raspberry Pi Pico specific code.
But it doesn't mean I recommend you to use it for any CMake projects. If you can use C/C++ libraries from Nim just by setting header files and libraries, you don't need to use hidecmakelinker. Use it only when using C/C++ libraries without writing CMake file is hard like Raspberry Pi Pico SDK.
## Requirements
- Nim 2.0.0
- Nimble is not required!
- [pathX](https://github.com/demotomohiro/pathX)
- CMake 3.13 or newer
## How to install
```console
$ nimble install https://github.com/demotomohiro/hidecmakelinker
```
or
```console
$ https://github.com/demotomohiro/hidecmakelinker.git
$ cd hidecmakelinker
$ nimble build
# Copy `src` directory or set Nim import path so that you can import modules in `src` directory
```
or
```console
$ https://github.com/demotomohiro/hidecmakelinker.git
$ cd hidecmakelinker/src
$ nim c hidecmakelinker.nim
$ nim c void.nim
# Copy `src` directory or set Nim import path so that you can import modules in `src` directory
```
`nimble install` or `nimble build` command produces 2 executable files, `hidecmakelinker` and `void`.
Make sure that they are in the one of directories in PATH environment variable and you can call them from console.
## How to use
Create `config.nims` in the same directory as your `*.nim` files and write following content:
```nim
switch("gcc.linkerexe", "hidecmakelinker")
switch("gcc.cpp.linkerexe", "hidecmakelinker")
switch("gcc.exe", "void")
switch("gcc.cpp.exe", "void")
nimcacheDir().mkDir()
```
If you use backend compiler other than gcc, you need to change 'gcc' with the compiler name you use.
And Nim's backend compiler must be same to C/C++ compiler called by CMake.
In the main module (when you compile Nim code with `nim c foo.nim`, `foo.nim` is a main module), add following code:
```nim
import hidecmakelinkerpkg/libconf
# Call this template after importing all modules.
writeHideCMakeToFile()
```
Then, you can build `*.nim` file with `nim c foo.nim` or `nim cpp foo.nim`.
## How to wrap C/C++ libraries for hidecmakelinker
In the Nim module that wrap C/C++ libraries, import `hidecmakelinkerpkg/libconf` module, create `LibParams` object by calling `initLibParams` and pass it to `config` template.
`LibParams` object contains information to generate `CMakeLists.txt` so that C/C++ libraries are linked to Nim code.
All parameters of `initLibParams` are optional.
List of parameters of `initLibParams`:
- `backendLang: BackendLang = blC`
- Set `blCpp` when wrapping C++ libraries so that Nim code is compiled with C++ backend
- `cmakeProgLangs: set[CMakeProgLang] = {cmplC, cmplCXX}`
- Selects which programming languages (excepts Nim) are needed to build the project
- Some Raspberry Pi Pico related libraries requires `cmplASM`
- They are added to `project` command as LANGUAGES option in generated CMakeLists.txt. See: https://cmake.org/cmake/help/latest/command/project.html
- `linkLibraries: openArray[string] = []`
- Library names used in CMakeLists.txt
- They are linked to Nim generated C/C++ code with `target_link_libraries` CMake command
- `cmakeStmts: openArray[(string, CMakeStmt)] = []`
- Adds CMake commands to generated CMakeLists.txt
- `CMakeStmt` have `name, depend: string`
- `name` can be any string but doesn't start with "std." as it is reserved by hidecmakelinker
- `depend` must be one of `cmakeStmts`'s `name` or names predefined by hidecmakelinker
- If `depend` is `foo`, it is inserted to `CMakeLists.txt` after `CMakeStmt` that has `name` match to `foo` was inserted
- `depend` can refer to any name in other imported module
- If `name` is empty string, `cmakesStmts`'s `depend` cannot refer to it
- If `depend` is empty string, it is inserted to `CMakeLists.txt` as soon as possible.
- When the CMake command must be placed after `project` CMake command, set `std.project` to `depend`
- Following procedures creates a CMake command
- `initCMakeInclude*(path: string; name = "", depend = ""): (string, CMakeStmt)`
- Generates `include` command
- https://cmake.org/cmake/help/latest/command/include.html
- `initCMakeCmd*(cmd: string; name = "", depend = ""): (string, CMakeStmt) =`
- Generates any CMake command specified by `cmd`
- `initCMakeCmdWithTarget*(cmd: string; name = "", depend = ""): (string, CMakeStmt) =`
- Generates any CMake command specified by `cmd` that refer to CMake target corresponding to Nim main module
- For example, if `cmd` is `pico_add_extra_outputs(#target)` and target name was `mynimcode`, it inserts `pico_add_extra_outputs(mynimcode)` in `CMakeLists.txt` after that target was defined
## Supported Nim compiler options
hidecmakelinker supports following Nim's compile options:
- `-d:release` and `-d:danger`
- Enables optimization
- `--opt:size` optimize for speed
- Uses [`CMAKE_BUILD_TYPE`](https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html)
- `-d:lto`
- Enables link time optimization
- Uses [`INTERPROCEDURAL_OPTIMIZATION`](https://cmake.org/cmake/help/latest/prop_tgt/INTERPROCEDURAL_OPTIMIZATION.html)
## How it works
At compile time, imported Nim modules wrapping C/C++ libraries tells information that is used to generated `CMakeLists.txt`.
It is done by putting that information (library target names, CMake commands, whether it requires C++ compiler or etc) to `LibParams` object and pass it to `config` template in libconf module.
They are gathered and saved to the file in nimcache directory when `writeHideCMakeToFile` template is called in main module.
After Nim executes compile time code and generates C/C++ code, Nim compiler trys to call backend C or C++ compiler to compile them.
But backend compiler executable name is replaced to `void` in `config.nims` so that backend compiler is not called and nothing happens. Nim generated C/C++ code are compiled by CMake later.
(So `void` can be any programs)
Then Nim compiler calls backend linker, but it is also replaced to hidecmakelinker in `config.nims`.
hidecmakelinker gets same arguments as backend linker called by Nim.
So hidecmakelinker can get a list of all C/C++ files that is needed to generating `CMakeLists.txt`.
It also read the file created by `writeHideCMakeToFile`.
Then, hidecmakelinker generates `CMakeLists.txt` in Nim cache directory and call CMake to build the project.
All temporary files are in Nim cache directory.
`CMakeLists.txt` is generated everytime you run `nim c foo.nim`, but build time can be shorter next time because CMake reuses compiled object files.
近期下载者:
相关文件:
收藏者: