Skip to content

Commit 697107c

Browse files
committed
build: Add CMake-based build system
1 parent 44c2452 commit 697107c

8 files changed

+281
-4
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,6 @@ build-aux/compile
6262
build-aux/test-driver
6363
src/stamp-h1
6464
libsecp256k1.pc
65+
66+
# Default CMake build directory.
67+
/build

CMakeLists.txt

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Copyright 2022
2+
# Distributed under the MIT software license, see the accompanying
3+
# file COPYING or https://www.opensource.org/licenses/mit-license.php.
4+
5+
cmake_minimum_required(VERSION 3.1)
6+
project(secp256k1 VERSION 0.1.0 LANGUAGES C)
7+
8+
set(CMAKE_C_STANDARD 90)
9+
set(CMAKE_C_STANDARD_REQUIRED ON)
10+
set(CMAKE_C_EXTENSIONS OFF)
11+
12+
option(ENABLE_DEV_MODE "enable all binaries and modules by default but individual options can still be overridden explicitly" OFF)
13+
14+
option(BUILD_BENCHMARK "compile benchmark" ON)
15+
option(BUILD_TESTS "compile tests" ON)
16+
option(BUILD_EXHAUSTIVE_TESTS "compile exhaustive tests" ON)
17+
option(BUILD_EXAMPLES "compile examples" ${ENABLE_DEV_MODE})
18+
19+
option(ENABLE_MODULE_ECDH "enable ECDH module" ${ENABLE_DEV_MODE})
20+
option(ENABLE_MODULE_RECOVERY "enable ECDSA pubkey recovery module" ${ENABLE_DEV_MODE})
21+
option(ENABLE_MODULE_EXTRAKEYS "enable extrakeys module" ${ENABLE_DEV_MODE})
22+
option(ENABLE_MODULE_SCHNORRSIG "enable schnorrsig module" ${ENABLE_DEV_MODE})
23+
24+
option(ALLOW_EXPERIMENTAL "allow experimental configure options" ${ENABLE_DEV_MODE})
25+
option(USE_EXTERNAL_DEFAULT_CALLBACKS "enable external default callback functions" OFF)
26+
option(COVERAGE "enable compiler flags to support kcov coverage analysis" ${ENABLE_DEV_MODE})
27+
28+
set(ECMULT_WINDOW_SIZE "auto" CACHE STRING "window size for ecmult precomputation for verification, specified as integer in range [2..24]. \"auto\" is a reasonable setting for desktop machines (currently 15). [default=auto]")
29+
if(ECMULT_WINDOW_SIZE STREQUAL auto)
30+
set(ECMULT_WINDOW_SIZE 15)
31+
endif()
32+
if(NOT ECMULT_WINDOW_SIZE MATCHES ^[1-9][0-9]*$ OR ECMULT_WINDOW_SIZE LESS 2 OR ECMULT_WINDOW_SIZE GREATER 24)
33+
message(FATAL_ERROR "ECMULT_WINDOW_SIZE value is \"${ECMULT_WINDOW_SIZE}\", but must an integer in range [2..24] or \"auto\".")
34+
endif()
35+
36+
set(ECMULT_GEN_PREC_BITS "auto" CACHE STRING "Precision bits to tune the precomputed table size for signing, specified as integer 2, 4 or 8. \"auto\" is a reasonable setting for desktop machines (currently 4). [default=auto]")
37+
if(ECMULT_GEN_PREC_BITS STREQUAL auto)
38+
set(ECMULT_GEN_PREC_BITS 4)
39+
endif()
40+
if(NOT ECMULT_GEN_PREC_BITS STREQUAL 2 AND NOT ECMULT_GEN_PREC_BITS STREQUAL 4 AND NOT ECMULT_GEN_PREC_BITS STREQUAL 8)
41+
message(FATAL_ERROR "ECMULT_GEN_PREC_BITS value is \"${ECMULT_GEN_PREC_BITS}\", but must an integer 2, 4, 8, or \"auto\".")
42+
endif()
43+
44+
option(USE_FORCE_WIDEMUL_INT128 "force the use of the (unsigned) __int128 based wide multiplication implementation" OFF)
45+
option(USE_FORCE_WIDEMUL_INT64 "force the use of the (u)int64_t based wide multiplication implementation" OFF)
46+
if (USE_FORCE_WIDEMUL_INT128 AND USE_FORCE_WIDEMUL_INT64)
47+
message(FATAL_ERROR "USE_FORCE_WIDEMUL_INT128 and USE_FORCE_WIDEMUL_INT64 cannot be enabled simultaneously.")
48+
endif()
49+
50+
configure_file(
51+
"cmake/libsecp256k1-config.h.in"
52+
"${PROJECT_BINARY_DIR}/src/libsecp256k1-config.h"
53+
)
54+
55+
include(cmake/secp_try_append_cflag.cmake)
56+
secp_try_append_cflag(-pedantic)
57+
secp_try_append_cflag(-Wno-long-long)
58+
secp_try_append_cflag(-Wnested-externs)
59+
secp_try_append_cflag(-Wshadow)
60+
secp_try_append_cflag(-Wstrict-prototypes)
61+
secp_try_append_cflag(-Wundef)
62+
secp_try_append_cflag(-Wno-overlength-strings)
63+
secp_try_append_cflag(-Wall)
64+
secp_try_append_cflag(-Wno-unused-function)
65+
secp_try_append_cflag(-Wextra)
66+
secp_try_append_cflag(-Wcast-align)
67+
secp_try_append_cflag(-Wcast-align=strict)
68+
secp_try_append_cflag(-Wconditional-uninitialized)
69+
70+
if(CMAKE_VERSION VERSION_GREATER 3.2)
71+
cmake_policy(SET CMP0063 NEW)
72+
endif()
73+
set(CMAKE_C_VISIBILITY_PRESET hidden)
74+
set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)
75+
76+
add_subdirectory(src)
77+
78+
message("\n")
79+
message("Configure summary")
80+
message("=================")
81+
if(CMAKE_CROSSCOMPILING)
82+
message("Cross compiling for ${CMAKE_SYSTEM_NAME}, ${CMAKE_SYSTEM_PROCESSOR}")
83+
endif()
84+
message("Optional binaries:")
85+
message(" benchmark ........................ ${BUILD_BENCHMARK}")
86+
message(" tests ............................ ${BUILD_TESTS}")
87+
message(" exhaustive tests ................. ${BUILD_EXHAUSTIVE_TESTS}")
88+
message(" examples ......................... ${BUILD_EXAMPLES}")
89+
message("Optional modules:")
90+
message(" ECDH ............................. ${ENABLE_MODULE_ECDH}")
91+
message(" ECDSA pubkey recovery ............ ${ENABLE_MODULE_RECOVERY}")
92+
message(" extrakeys ........................ ${ENABLE_MODULE_EXTRAKEYS}")
93+
message(" schnorrsig ....................... ${ENABLE_MODULE_SCHNORRSIG}")
94+
message("Parameters:")
95+
message(" ecmult window size ............... ${ECMULT_WINDOW_SIZE}")
96+
message(" ecmult gen precision bits ........ ${ECMULT_GEN_PREC_BITS}")
97+
message("Optional features:")
98+
message(" external callbacks ............... ${USE_EXTERNAL_DEFAULT_CALLBACKS}")
99+
if(BUILD_TESTS OR BUILD_EXHAUSTIVE_TESTS)
100+
message(" support kcov coverage analysis ... ${COVERAGE}")
101+
endif()
102+
if(USE_FORCE_WIDEMUL_INT128)
103+
message(" override wide multiplication ..... int128")
104+
endif()
105+
if(USE_FORCE_WIDEMUL_INT64)
106+
message(" override wide multiplication ..... int64")
107+
endif()
108+
message("\n")
109+
message("CC: ${CMAKE_C_COMPILER}")
110+
message("CFLAGS: ${CMAKE_C_FLAGS}")
111+
message("\n")

README.md

+34-4
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,8 @@ Implementation details
5757
* Optional runtime blinding which attempts to frustrate differential power analysis.
5858
* The precomputed tables add and eventually subtract points for which no known scalar (secret key) is known, preventing even an attacker with control over the secret key used to control the data internally.
5959

60-
Build steps
61-
-----------
62-
63-
libsecp256k1 is built using autotools:
60+
Building with Autotools
61+
-----------------------
6462

6563
$ ./autogen.sh
6664
$ ./configure
@@ -70,6 +68,38 @@ libsecp256k1 is built using autotools:
7068

7169
To compile optional modules (such as Schnorr signatures), you need to run `./configure` with additional flags (such as `--enable-module-schnorrsig`). Run `./configure --help` to see the full list of available flags.
7270

71+
Building with CMake
72+
-------------------
73+
74+
To maintain a pristine source tree, CMake encourages to perform an out-of-source build by using a separate dedicated build tree.
75+
76+
$ rm -rf build && mkdir build && cd build
77+
$ cmake ..
78+
$ make
79+
$ sudo make install # optional
80+
81+
To adjust the build system configuration, the following options can be provided to `cmake` call:
82+
83+
| Option | Default value | Description |
84+
|--------|:-------------:|-------------|
85+
| BUILD_BENCHMARK | ON | compile benchmark |
86+
| BUILD_TESTS | ON | compile tests |
87+
| BUILD_EXHAUSTIVE_TESTS | ON | compile exhaustive tests |
88+
| BUILD_EXAMPLES | OFF | compile examples |
89+
| ENABLE_MODULE_ECDH | OFF | enable ECDH module |
90+
| ENABLE_MODULE_RECOVERY | OFF | enable ECDSA pubkey recovery module |
91+
| ENABLE_MODULE_EXTRAKEYS | OFF |enable extrakeys module |
92+
| ENABLE_MODULE_SCHNORRSIG | OFF | enable schnorrsig module |
93+
94+
For example:
95+
96+
$ cmake -DBUILD_BENCHMARK=OFF -DENABLE_MODULE_ECDH=ON ..
97+
98+
To cross compile, preconfigured toolchain files are available in the `toolchain` directory.
99+
For example, to cross compile for Windows:
100+
101+
$ cmake -DCMAKE_TOOLCHAIN_FILE=../toolchain/x86_64-w64-mingw32.cmake ..
102+
73103
Usage examples
74104
-----------
75105
Usage examples can be found in the [examples](examples) directory. To compile them you need to configure with `--enable-examples`.

cmake/libsecp256k1-config.h.in

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/***********************************************************************
2+
* Copyright (c) 2022 *
3+
* Distributed under the MIT software license, see the accompanying *
4+
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
5+
***********************************************************************/
6+
7+
#ifndef LIBSECP256K1_CONFIG_H
8+
9+
#define LIBSECP256K1_CONFIG_H
10+
11+
/* Define this symbol to enable the ECDH module */
12+
#cmakedefine ENABLE_MODULE_ECDH
13+
14+
/* Define this symbol to enable the ECDSA pubkey recovery module */
15+
#cmakedefine ENABLE_MODULE_RECOVERY
16+
17+
/* Define this symbol to enable the extrakeys module */
18+
#cmakedefine ENABLE_MODULE_EXTRAKEYS
19+
20+
/* Define this symbol to enable the schnorrsig module */
21+
#cmakedefine ENABLE_MODULE_SCHNORRSIG
22+
23+
/* Define this symbol if an external implementation of the default callbacks
24+
is used */
25+
#cmakedefine USE_EXTERNAL_DEFAULT_CALLBACKS
26+
27+
/* Define this symbol to compile out all VERIFY code */
28+
#cmakedefine COVERAGE
29+
30+
/* Set window size for ecmult precomputation */
31+
#cmakedefine ECMULT_WINDOW_SIZE @ECMULT_WINDOW_SIZE@
32+
33+
/* Set ecmult gen precision bits */
34+
#cmakedefine ECMULT_GEN_PREC_BITS @ECMULT_GEN_PREC_BITS@
35+
36+
/* Define this symbol if valgrind is installed, and it supports the host
37+
platform */
38+
#cmakedefine HAVE_VALGRIND
39+
40+
/* Define this symbol to enable x86_64 assembly optimizations */
41+
#cmakedefine USE_ASM_X86_64
42+
43+
/* Define this symbol if an external (non-inline) assembly implementation is
44+
used */
45+
#cmakedefine USE_EXTERNAL_ASM
46+
47+
/* Define this symbol to force the use of the (unsigned) __int128 based wide
48+
multiplication implementation */
49+
#cmakedefine USE_FORCE_WIDEMUL_INT128
50+
51+
/* Define this symbol to force the use of the (u)int64_t based wide
52+
multiplication implementation */
53+
#cmakedefine USE_FORCE_WIDEMUL_INT64
54+
55+
#endif /*LIBSECP256K1_CONFIG_H*/

cmake/secp_try_append_cflag.cmake

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright 2022
2+
# Distributed under the MIT software license, see the accompanying
3+
# file COPYING or https://www.opensource.org/licenses/mit-license.php.
4+
5+
include(CheckCCompilerFlag)
6+
7+
function(secp_try_append_cflag dashed_flag)
8+
string(REGEX REPLACE "^(-)" "" flag ${dashed_flag})
9+
check_c_compiler_flag(${dashed_flag} ${flag})
10+
if(${flag})
11+
string(STRIP "${CMAKE_C_FLAGS} ${dashed_flag}" CMAKE_C_FLAGS)
12+
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} PARENT_SCOPE)
13+
endif()
14+
endfunction()

src/CMakeLists.txt

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2022
2+
# Distributed under the MIT software license, see the accompanying
3+
# file COPYING or https://www.opensource.org/licenses/mit-license.php.
4+
5+
add_definitions(-DHAVE_CONFIG_H)
6+
include_directories(${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/include)
7+
include_directories(${PROJECT_BINARY_DIR}/src)
8+
9+
add_library(secp256k1 "")
10+
target_sources(secp256k1
11+
PRIVATE
12+
secp256k1.c
13+
precomputed_ecmult.c
14+
precomputed_ecmult_gen.c
15+
)
16+
17+
if(BUILD_BENCHMARK)
18+
add_executable(bench bench.c)
19+
target_link_libraries(bench PRIVATE secp256k1)
20+
add_executable(bench_internal bench_internal.c)
21+
target_link_libraries(bench_internal PRIVATE secp256k1)
22+
add_executable(bench_ecmult bench_ecmult.c)
23+
target_link_libraries(bench_ecmult PRIVATE secp256k1)
24+
endif()
25+
26+
if(BUILD_TESTS)
27+
add_executable(tests tests.c)
28+
if(NOT COVERAGE)
29+
target_compile_definitions(tests PRIVATE -DVERIFY)
30+
endif()
31+
target_link_libraries(tests PRIVATE secp256k1)
32+
target_link_options(tests
33+
PRIVATE
34+
$<$<NOT:$<PLATFORM_ID:Darwin>>:-static>
35+
)
36+
endif()
37+
38+
if(BUILD_EXHAUSTIVE_TESTS)
39+
add_executable(exhaustive_tests tests_exhaustive.c)
40+
if(NOT COVERAGE)
41+
target_compile_definitions(exhaustive_tests PRIVATE -DVERIFY)
42+
endif()
43+
target_link_libraries(exhaustive_tests PRIVATE secp256k1)
44+
target_link_options(tests
45+
PRIVATE
46+
$<$<NOT:$<PLATFORM_ID:Darwin>>:-static>
47+
)
48+
endif()
49+
50+
install(TARGETS secp256k1)

toolchain/arm-linux-gnueabihf.cmake

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright 2022
2+
# Distributed under the MIT software license, see the accompanying
3+
# file COPYING or https://www.opensource.org/licenses/mit-license.php.
4+
5+
set(CMAKE_SYSTEM_NAME Linux)
6+
set(CMAKE_SYSTEM_PROCESSOR arm)
7+
set(CMAKE_C_COMPILER arm-linux-gnueabihf-gcc)

toolchain/x86_64-w64-mingw32.cmake

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright 2022
2+
# Distributed under the MIT software license, see the accompanying
3+
# file COPYING or https://www.opensource.org/licenses/mit-license.php.
4+
5+
set(CMAKE_SYSTEM_NAME Windows)
6+
set(CMAKE_SYSTEM_PROCESSOR x86_64)
7+
set(CMAKE_C_COMPILER x86_64-w64-mingw32-gcc)

0 commit comments

Comments
 (0)