From 3b20a4e16594b19a38f006a7af051c775bf0e1c9 Mon Sep 17 00:00:00 2001 From: Rene Rivera Date: Mon, 29 Sep 2025 19:51:24 -0500 Subject: [PATCH] Add initial support for {CPP}-26 Contracts for GCC based toolsets (like clang). --- doc/src/history.adoc | 6 +++ doc/src/reference.adoc | 1 + example/contracts/hello.cpp | 21 +++++++++++ example/contracts/jamroot.jam | 14 +++++++ example/contracts/readme.adoc | 47 +++++++++++++++++++++++ src/build/version.jam | 4 +- src/engine/patchlevel.h | 4 +- src/tools/features/contracts-feature.jam | 48 ++++++++++++++++++++++++ src/tools/gcc.jam | 9 ++++- 9 files changed, 149 insertions(+), 5 deletions(-) create mode 100644 example/contracts/hello.cpp create mode 100644 example/contracts/jamroot.jam create mode 100644 example/contracts/readme.adoc create mode 100644 src/tools/features/contracts-feature.jam diff --git a/doc/src/history.adoc b/doc/src/history.adoc index ce52d2f45..888dfeb46 100644 --- a/doc/src/history.adoc +++ b/doc/src/history.adoc @@ -1,6 +1,12 @@ [[b2.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 * Fix `stdlib=libc++` feature being applied to compiles other than {CPP}. diff --git a/doc/src/reference.adoc b/doc/src/reference.adoc index 4ef51d478..76fb92ee3 100644 --- a/doc/src/reference.adoc +++ b/doc/src/reference.adoc @@ -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/lto-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]] == Builtin tools diff --git a/example/contracts/hello.cpp b/example/contracts/hello.cpp new file mode 100644 index 000000000..bcbb43fcf --- /dev/null +++ b/example/contracts/hello.cpp @@ -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 +#include + +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[] diff --git a/example/contracts/jamroot.jam b/example/contracts/jamroot.jam new file mode 100644 index 000000000..039cf4343 --- /dev/null +++ b/example/contracts/jamroot.jam @@ -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 + on + enforce + clang:libc++ + 23 + ; + +exe hello : hello.cpp ; diff --git a/example/contracts/readme.adoc b/example/contracts/readme.adoc new file mode 100644 index 000000000..c83e87f51 --- /dev/null +++ b/example/contracts/readme.adoc @@ -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. diff --git a/src/build/version.jam b/src/build/version.jam index c3ea219a0..516ab928e 100644 --- a/src/build/version.jam +++ b/src/build/version.jam @@ -9,8 +9,8 @@ import numbers ; # Mirror engine JAM_VERSION .major = 5 ; -.minor = 3 ; -.patch = 3 ; +.minor = 4 ; +.patch = 0 ; rule build ( ) diff --git a/src/engine/patchlevel.h b/src/engine/patchlevel.h index 6807d085b..dd5c16fb7 100644 --- a/src/engine/patchlevel.h +++ b/src/engine/patchlevel.h @@ -13,5 +13,5 @@ https://www.bfgroup.xyz/b2/LICENSE.txt) */ #define VERSION_MAJOR 5 -#define VERSION_MINOR 3 -#define VERSION_PATCH 3 +#define VERSION_MINOR 4 +#define VERSION_PATCH 0 diff --git a/src/tools/features/contracts-feature.jam b/src/tools/features/contracts-feature.jam new file mode 100644 index 000000000..97268bba8 --- /dev/null +++ b/src/tools/features/contracts-feature.jam @@ -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 `clang:libc++` and `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 + : ; diff --git a/src/tools/gcc.jam b/src/tools/gcc.jam index 2b6caed41..fecc8df9d 100644 --- a/src/tools/gcc.jam +++ b/src/tools/gcc.jam @@ -1,6 +1,6 @@ # Copyright 2021 Nikita Kniazev # Copyright 2001 David Abrahams -# Copyright 2002-2017 Rene Rivera +# Copyright 2002-2025 René Ferdinand Rivera Morell # Copyright 2002-2003 Vladimir Prus # Copyright 2005 Reece H. Dunn # Copyright 2006 Ilya Sokolov @@ -716,6 +716,13 @@ toolset.flags gcc.link OPTIONS on/fat : -flto ; toolset.flags gcc.compile.c++ DEFINES gnu : _GLIBCXX_USE_CXX11_ABI=0 ; toolset.flags gcc.compile.c++ DEFINES gnu11 : _GLIBCXX_USE_CXX11_ABI=1 ; +# Contracts +toolset.flags gcc.compile.c++ OPTIONS on : -fcontracts ; +toolset.flags gcc.compile.c++ OPTIONS on/ignore : -fcontract-evaluation-semantic=ignore ; +toolset.flags gcc.compile.c++ OPTIONS on/observe : -fcontract-evaluation-semantic=observe ; +toolset.flags gcc.compile.c++ OPTIONS on/enforce : -fcontract-evaluation-semantic=enforce ; +toolset.flags gcc.compile.c++ OPTIONS on/quick_enforce : -fcontract-evaluation-semantic=quick_enforce ; + ### ### User free feature options. ###