Skip to content

Commit 7b12a7b

Browse files
committed
completed build2 instructions and examples
Signed-off-by: Joël Lamotte <[email protected]>
1 parent 3939c55 commit 7b12a7b

File tree

5 files changed

+143
-15
lines changed

5 files changed

+143
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The files in this directory are for documentation only. In the current file/directory configuration they cannot be used as a `build2` project. Use `bdep new` to create a proper project and replace the files where necessary. See [../package_managers.md#build2] for details.
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: example
2+
version: 0.1.0-a.0.z
3+
language: c++
4+
summary: example C++ executable
5+
license: other: proprietary ; Not free/open source.
6+
description-file: README.md
7+
url: https://example.org/example
8+
9+
#build-error-email: [email protected]
10+
depends: * build2 >= 0.16.0
11+
depends: * bpkg >= 0.16.0
12+
#depends: libhello ^1.0.0
13+
14+
depends: nlohmann-json ^3.0.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
: 1
2+
summary: example project repository
3+
4+
:
5+
role: prerequisite
6+
location: https://pkg.cppget.org/1/stable
7+
#trust: ...
8+
9+
#:
10+
#role: prerequisite
11+
#location: https://git.build2.org/hello/libhello.git
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
: json basics
3+
:
4+
$* >>~/EOO/
5+
{
6+
"compiler": {
7+
/ "c\+\+": "\d+",/
8+
/ "family": "[\w\d]+",/
9+
/ "version": \d+/
10+
},
11+
/ "copyright": "\(C\) 2013-\d+ Niels Lohmann",/
12+
"name": "JSON for Modern C++",
13+
/ "platform": "[\w\d]+",/
14+
"url": "https://github.com/nlohmann/json",
15+
"version": {
16+
"major": 3,
17+
/ "minor": \d+,/
18+
/ "patch": \d+,/
19+
/ "string": "3\.\d+\.\d+"/
20+
}
21+
}
22+
EOO
23+
24+

docs/mkdocs/docs/integration/package_managers.md

+92-15
Original file line numberDiff line numberDiff line change
@@ -703,19 +703,21 @@ to install the [nlohmann-json](https://ports.macports.org/port/nlohmann-json/) p
703703
!!! abstract "Summary"
704704

705705
package: **`nlohmann-json`**
706-
library target: **`nlohmann-json%`**
706+
library target: **`nlohmann-json%lib{json}`**
707707
available in package repositories:
708708
- [`cppget.org` (recommended)](https://cppget.org/nlohmann-json)
709709
- [package's sources (for advanced users)](https://github.com/build2-packaging/nlohmann-json/)
710710

711711
- :octicons-tag-24: Available versions: current version and older versions since `3.7.3` (see [cppget.org](https://cppget.org/nlohmann-json))
712-
- :octicons-rocket-24: The package is maintained and published by the community in [this the repository][(https://github.com/conan-io/conan-center-index/tree/master/recipes/nlohmann_json](https://github.com/build2-packaging/nlohmann-json/)).
712+
- :octicons-rocket-24: The package is maintained and published by the `build2` community in [this the repository][(https://github.com/conan-io/conan-center-index/tree/master/recipes/nlohmann_json](https://github.com/build2-packaging/nlohmann-json/)).
713713
- :octicons-file-24: File issues at the [package source repository](https://github.com/build2-packaging/nlohmann-json/issues/)
714714
- :octicons-question-24: [`build2` website](https://build2)
715715

716+
Note: [`build2`](https://build2.org) should not be considered as a standalone package-manager. It is a build-system + package manager + project manager, a set of tools that work hand-in-hand. `build2`-based projects do not rely on existing `CMake` scripts and the build scripts defining the project's targets are specific to `build2`.
717+
716718
To use this package in an exising [`build2`](https://build2.org) project, the general steps are:
717719

718-
0. <details><summary>Make the package available to download from a package repository that provides it.</b></summary>
720+
1. <details><summary>Make the package available to download from a package repository that provides it.</b></summary>
719721

720722
Your project's `repositories.manifest` specifies where the package manager will try to acquire packages by default. Make sure one of the repositories specified in this file provides `nlhomann-json` package.
721723
The recommended open-source repository is [`cppget.org`](https://cppget.org/).
@@ -728,7 +730,7 @@ To use this package in an exising [`build2`](https://build2.org) project, the ge
728730
```
729731
</details>
730732
731-
1. <details><summary>Add this package as dependency of your project.</summary>
733+
2. <details><summary>Add this package as dependency of your project.</summary>
732734
733735
In your project's `manifest` add the dependency to the package, like `depends: nlohmann-json`, probably with some [version constraints](https://build2.org/build2-toolchain/doc/build2-toolchain-intro.xhtml#guide-add-remove-deps).
734736
For example, to depend on the latest `3.x` version available:
@@ -738,12 +740,12 @@ To use this package in an exising [`build2`](https://build2.org) project, the ge
738740
</details>
739741
740742
741-
2. <details><summary>Add this library (from the package) as dependency of your target that uses it.</summary>
743+
2. <details><summary>Add this library as dependency of your target that uses it.</summary>
742744
743745
In the `buildfile` defining the target that will use this library:
744-
- import the target from the `nlhomann-json` package, for example:
746+
- import the target `lib{json}` from the `nlhomann-json` package, for example:
745747
```
746-
import nljson = nlhomann-json%json
748+
import nljson = nlhomann-json%lib{json}
747749
```
748750
- add the library's target as requirement for your target using it, for example:
749751
```
@@ -753,7 +755,7 @@ To use this package in an exising [`build2`](https://build2.org) project, the ge
753755
754756
3. <details><summary>Use the library in your project's code and build it.</summary>
755757
756-
At this point, any `b` or `bdep update` command that will update/build the project will also acquire the missing dependency automatically, then build it and link it with your target.
758+
At this point, assuming your project is initialized in a build-configuration, any `b` or `bdep update` command that will update/build the project will also acquire the missing dependency automatically, then build it and link it with your target.
757759
758760
If you just want to trigger the dependencies synchronization for all your configurations:
759761
```
@@ -762,15 +764,90 @@ To use this package in an exising [`build2`](https://build2.org) project, the ge
762764
</details>
763765
764766
??? example "Example: from scratch, using `build2`'s [`bdep new` command](https://build2.org/bdep/doc/bdep-new.xhtml)"
765-
```
766-
# create a simple executable project from scratch, with a simplfied directory layout, using `.hpp/.cpp` file extensions:
767-
bdep new example -exe,no-subdirs,no-tests -l c++,cpp
768767
769-
```
768+
1. Create a new executable project "example" (see [`bdep new` command details](https://build2.org/bdep/doc/bdep-new.xhtml) for the various options to create a new project):
770769
771-
```shell
772-
bdep new -t exe -l c++
773-
```
770+
```shell
771+
bdep new example
772+
```
773+
774+
2. Enable acquiring packages from https://cppget.org by uncommenting these lines in `example/repositories.manifest`:
775+
776+
```make
777+
:
778+
role: prerequisite
779+
location: https://pkg.cppget.org/1/stable
780+
```
781+
782+
Your `example/manifest` should now look like this:
783+
784+
```make title="project's `repositories.manifest`"
785+
--8<-- "integration/build2/repositories.manifest"
786+
```
787+
788+
3. Add any available `3.x` version of the `nlohmann-json` package as dependency to the project by adding this line to `example/manifest`:
789+
790+
```make
791+
depends: nlohmann-json ^3.0.0
792+
```
793+
794+
Your `example/manifest` should now look like this:
795+
796+
```make title="project's `manifest`"
797+
--8<-- "integration/build2/manifest"
798+
```
799+
800+
4. In `example/example/buildfile`, import the library's target to be used as requirement for building the executable target `exe{example}` by replacing the top lines of that file by:
801+
802+
```make
803+
libs =
804+
import libs = nlhomann-json%lib{json}
805+
806+
exe{example}: {hxx ixx txx cxx}{**} $libs testscript
807+
```
808+
809+
Your `example/example/buildfile` should now look like this:
810+
811+
```make title="project's `buildfile`"
812+
--8<-- "integration/build2/buildfile"
813+
```
814+
815+
5. Initialize the project in a C/C++ build configuration directory that we will create in the same command, with the default C++ build toolchain for simplicity:
816+
817+
```shell
818+
cd example/
819+
bdep init -C @myconfig cc
820+
```
821+
822+
Note that this will also download the project's dependencies, here the package `nlhomann-json` into the newly created build configuration directory `example-myconfig/`.
823+
824+
6. Replace the C++ source code in `example/example/example.cxx` by this code:
825+
826+
```cpp title="example.cxx"
827+
--8<-- "integration/build2/example.cpp"
828+
```
829+
830+
7. Build the project by using [`b`](https://build2.org/build2/doc/b.xhtml) or [`bdep update`](https://build2.org/bdep/doc/bdep.xhtml):
831+
832+
```shell
833+
# in example/
834+
b
835+
```
836+
837+
This should generate the executable in `example-myconfig/example/example/` and add a symbolic link to it in `example/example/`.
838+
839+
- If you want to be able to test that executable's output is correct, you can use build2's executable testing tooling `tescript` by changing the content of `example/example/testscript` to this code:
840+
841+
```cpp title="`testscript` checking the output of the program"
842+
--8<-- "integration/build2/testscript"
843+
```
844+
845+
Then run [`b test`](https://build2.org/build2/doc/b.xhtml) or [`bdep test`](https://build2.org/bdep/doc/bdep.xhtml) to automatically check the program's output. Assuming that the c++ code above didnt change, it should not fail as long as the library outputs the expected json format.
846+
847+
```shell
848+
# in example/
849+
b test
850+
```
774851
775852
## CPM.cmake
776853

0 commit comments

Comments
 (0)