2
0
mirror of https://github.com/boostorg/build.git synced 2026-01-19 04:02:14 +00:00

Add initial support for {CPP}-26 Contracts for GCC based toolsets (like clang).

This commit is contained in:
Rene Rivera
2025-09-29 19:51:24 -05:00
parent c803de9a3d
commit 3b20a4e165
9 changed files with 149 additions and 5 deletions

View File

@@ -1,6 +1,12 @@
[[b2.history]] [[b2.history]]
= History = History
== Version 5.4.0
* *New*: Add initial support for {CPP}-26 Contracts for GCC based toolsets
(like clang).
-- _René Ferdinand Rivera Morell_
== Version 5.3.3 == Version 5.3.3
* Fix `stdlib=libc++` feature being applied to compiles other than {CPP}. * Fix `stdlib=libc++` feature being applied to compiles other than {CPP}.

View File

@@ -286,6 +286,7 @@ include::../../src/tools/features/warnings-feature.jam[tag=doc]
include::../../src/tools/features/translate-path-feature.jam[tag=doc] include::../../src/tools/features/translate-path-feature.jam[tag=doc]
include::../../src/tools/features/lto-feature.jam[tag=doc] include::../../src/tools/features/lto-feature.jam[tag=doc]
include::../../src/tools/features/response-file-feature.jam[tag=doc] include::../../src/tools/features/response-file-feature.jam[tag=doc]
include::../../src/tools/features/contracts-feature.jam[tag=doc]
[[b2.reference.tools]] [[b2.reference.tools]]
== Builtin tools == Builtin tools

View File

@@ -0,0 +1,21 @@
// Copyright 2025 René Ferdinand Rivera Morel
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.txt or copy at
// https://www.bfgroup.xyz/b2/LICENSE.txt)
// tag::source[]
#include <contracts>
#include <iostream>
int f(const int x) pre(x != 0) post(r : r != x)
{
return x + 1;
}
int main()
{
std::cout << "Hello!\n";
f(1);
f(0);
}
// end::source[]

View File

@@ -0,0 +1,14 @@
# Copyright 2008 Jurko Gospodnetic
# Copyright 2025 René Ferdinand Rivera Morell
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE.txt or https://www.bfgroup.xyz/b2/LICENSE.txt)
project
: requirements
<contracts>on
<contracts-semantic>enforce
<toolset>clang:<stdlib>libc++
<cxxstd>23
;
exe hello : hello.cpp ;

View File

@@ -0,0 +1,47 @@
////
Copyright 2008 Jurko Gospodnetic
Copyright 2025 René Ferdinand Rivera Morell
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.txt or https://www.bfgroup.xyz/b2/LICENSE.txt)
////
= Hello
This example shows a very basic Boost Build project set up so it compiles a
single executable which makes use of contracts from a single source file:
.`hello.cpp`
[source,cpp]
----
include::../../example/contracts/hello.cpp[tag=source]
----
Our `jamroot.jam` is minimal and only specifies one `exe` target for the
program:
.`jamroot.jam`
[source,jam]
----
include::jamroot.jam[]
----
Building the example yields:
[source,bash]
----
> cd /example/hello
> b2
...found 12 targets...
...updating 8 targets...
clang-linux.compile.c++ bin/clang-linux-22.0~contracts/debug/contracts-on-enforce/cxxstd-23-iso/stdlib-libc++/hello.o
clang-linux.link bin/clang-linux-22.0~contracts/debug/contracts-on-enforce/cxxstd-23-iso/stdlib-libc++/hello
...updated 8 targets...
> LD_LIBRARY_PATH=/opt/clang-ericwf-contracts-trunk/lib/x86_64-unknown-linux-gnu/ bin/clang-linux-22.0~contracts/debug/contracts-on-enforce/cxxstd-23-iso/stdlib-libc++/hello
Hello!
hello.cpp:11: pre(x != 0) failed
libc++abi: terminating
Aborted (core dumped) LD_LIBRARY_PATH=/opt/clang-ericwf-contracts-trunk/lib/x86_64-unknown-linux-gnu/ bin/clang-linux-22.0~contracts/debug/contracts-on-enforce/cxxstd-23-iso/stdlib-libc++/hello
----
NOTE: The actual paths will depend on your configuration.

View File

@@ -9,8 +9,8 @@ import numbers ;
# Mirror engine JAM_VERSION # Mirror engine JAM_VERSION
.major = 5 ; .major = 5 ;
.minor = 3 ; .minor = 4 ;
.patch = 3 ; .patch = 0 ;
rule build ( ) rule build ( )

View File

@@ -13,5 +13,5 @@ https://www.bfgroup.xyz/b2/LICENSE.txt)
*/ */
#define VERSION_MAJOR 5 #define VERSION_MAJOR 5
#define VERSION_MINOR 3 #define VERSION_MINOR 4
#define VERSION_PATCH 3 #define VERSION_PATCH 0

View File

@@ -0,0 +1,48 @@
# Copyright 2025 René Ferdinand Rivera Morel
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE.txt or copy at
# https://www.bfgroup.xyz/b2/LICENSE.txt)
import feature ;
#| tag::doc[]
[[b2.builtin.features.contracts]]`contracts`::
*Allowed values:* `on`.
+
Enables use of experimental use of {CPP}-26 Contracts (P2900). Enabling
contracts has additional requirements when using them with at least clang. See
https://contracts.efcs.ca/ for details. For B2 the requirements amount to
needing to use `<toolset>clang:<stdlib>libc++` and `<cxxstd>23`.
|# # end::doc[]
feature.feature contracts
: on
: optional propagated link-incompatible ;
#| tag::doc[]
[[b2.builtin.features.contracts-sematic]]`contracts-semantic`::
*Subfeature of* `contracts`
+
*Allowed values:* `enforce', 'quick_enforce', 'observe', 'ignore`.
+
Specifies the contract checking semantic for the translation unit. The default
is `enforce` for which contract checks are evaluated with violations resulting
in error termination.
+
`enforce`::: Contract assertions are not evaluated (contracts are disabled).
`quick_enforce`::: Contract assertions are evaluated but violations do not
terminate the program.
`observe`::: Contract assertions are evaluated and violations terminate the
program.
`ignore`::: Like enforce, but uses optimized evaluation that may skip some
checks.
|# # end::doc[]
feature.subfeature contracts
: semantic
: enforce quick_enforce observe ignore
: ;

View File

@@ -1,6 +1,6 @@
# Copyright 2021 Nikita Kniazev # Copyright 2021 Nikita Kniazev
# Copyright 2001 David Abrahams # Copyright 2001 David Abrahams
# Copyright 2002-2017 Rene Rivera # Copyright 2002-2025 René Ferdinand Rivera Morell
# Copyright 2002-2003 Vladimir Prus # Copyright 2002-2003 Vladimir Prus
# Copyright 2005 Reece H. Dunn # Copyright 2005 Reece H. Dunn
# Copyright 2006 Ilya Sokolov # Copyright 2006 Ilya Sokolov
@@ -716,6 +716,13 @@ toolset.flags gcc.link OPTIONS <lto>on/<lto-mode>fat : -flto ;
toolset.flags gcc.compile.c++ DEFINES <stdlib>gnu : _GLIBCXX_USE_CXX11_ABI=0 ; toolset.flags gcc.compile.c++ DEFINES <stdlib>gnu : _GLIBCXX_USE_CXX11_ABI=0 ;
toolset.flags gcc.compile.c++ DEFINES <stdlib>gnu11 : _GLIBCXX_USE_CXX11_ABI=1 ; toolset.flags gcc.compile.c++ DEFINES <stdlib>gnu11 : _GLIBCXX_USE_CXX11_ABI=1 ;
# Contracts
toolset.flags gcc.compile.c++ OPTIONS <contracts>on : -fcontracts ;
toolset.flags gcc.compile.c++ OPTIONS <contracts>on/<contracts-semantic>ignore : -fcontract-evaluation-semantic=ignore ;
toolset.flags gcc.compile.c++ OPTIONS <contracts>on/<contracts-semantic>observe : -fcontract-evaluation-semantic=observe ;
toolset.flags gcc.compile.c++ OPTIONS <contracts>on/<contracts-semantic>enforce : -fcontract-evaluation-semantic=enforce ;
toolset.flags gcc.compile.c++ OPTIONS <contracts>on/<contracts-semantic>quick_enforce : -fcontract-evaluation-semantic=quick_enforce ;
### ###
### User free feature options. ### User free feature options.
### ###