Skip to content

Commit b91882b

Browse files
authored
Use cmake for building (#90)
* Use cmake for building * CMakeLists.txt modified to support full standalone build * added support for ARCH=native builds * added PowerPC flags * added ARMv8 flags * check for x86 AES-NI at compile time
1 parent 4a4b06e commit b91882b

File tree

5 files changed

+148
-240
lines changed

5 files changed

+148
-240
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ obj/
55
.vs
66
x64/
77
Release/
8-
Debug/
8+
Debug/
9+
build/

CMakeLists.txt

+104-9
Original file line numberDiff line numberDiff line change
@@ -51,31 +51,126 @@ src/virtual_machine.cpp
5151
src/vm_compiled_light.cpp
5252
src/blake2/blake2b.c)
5353

54-
if (NOT ARCH_ID)
55-
set(ARCH_ID ${CMAKE_HOST_SYSTEM_PROCESSOR})
54+
if(NOT ARCH_ID)
55+
# allow cross compiling
56+
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "")
57+
set(CMAKE_SYSTEM_PROCESSOR ${CMAKE_HOST_SYSTEM_PROCESSOR})
58+
endif()
59+
string(TOLOWER "${CMAKE_SYSTEM_PROCESSOR}" ARCH_ID)
60+
endif()
61+
62+
if(NOT ARM_ID)
63+
set(ARM_ID "${ARCH_ID}")
64+
endif()
65+
66+
if(NOT ARCH)
67+
set(ARCH "default")
5668
endif()
5769

5870
if(NOT CMAKE_BUILD_TYPE)
5971
set(CMAKE_BUILD_TYPE Release)
72+
message(STATUS "Setting default build type: ${CMAKE_BUILD_TYPE}")
6073
endif()
6174

75+
include(CheckCXXCompilerFlag)
76+
include(CheckCCompilerFlag)
77+
78+
function(add_flag flag)
79+
string(REPLACE "-" "_" supported_cxx ${flag}_cxx)
80+
check_cxx_compiler_flag(${flag} ${supported_cxx})
81+
if(${${supported_cxx}})
82+
message(STATUS "Setting CXX flag ${flag}")
83+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}" PARENT_SCOPE)
84+
endif()
85+
string(REPLACE "-" "_" supported_c ${flag}_c)
86+
check_c_compiler_flag(${flag} ${supported_c})
87+
if(${${supported_c}})
88+
message(STATUS "Setting C flag ${flag}")
89+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}" PARENT_SCOPE)
90+
endif()
91+
endfunction()
92+
93+
# x86-64
6294
if (ARCH_ID STREQUAL "x86_64" OR ARCH_ID STREQUAL "x86-64" OR ARCH_ID STREQUAL "amd64")
6395
list(APPEND randomx_sources
6496
src/jit_compiler_x86_static.S
6597
src/jit_compiler_x86.cpp)
66-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes")
67-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes")
98+
# cheat because cmake and ccache hate each other
99+
set_property(SOURCE src/jit_compiler_x86_static.S PROPERTY LANGUAGE C)
100+
101+
if(ARCH STREQUAL "native")
102+
add_flag("-march=native")
103+
else()
104+
# default build has hardware AES enabled (software AES can be selected at runtime)
105+
add_flag("-maes")
106+
endif()
107+
endif()
108+
109+
# PowerPC
110+
if (ARCH_ID STREQUAL "ppc64" OR ARCH_ID STREQUAL "ppc64le")
111+
if(ARCH STREQUAL "native")
112+
add_flag("-mcpu=native")
113+
endif()
114+
# PowerPC AES requires ALTIVEC (POWER7+), so it cannot be enabled in the default build
115+
endif()
116+
117+
# ARMv8
118+
if (ARM_ID STREQUAL "aarch64" OR ARM_ID STREQUAL "arm64" OR ARM_ID STREQUAL "armv8-a")
119+
if(ARCH STREQUAL "native")
120+
add_flag("-march=native")
121+
else()
122+
# default build has hardware AES enabled (software AES can be selected at runtime)
123+
add_flag("-march=armv8-a+crypto")
124+
endif()
68125
endif()
69126

70127
set(RANDOMX_INCLUDE "${CMAKE_CURRENT_SOURCE_DIR}/src" CACHE STRING "RandomX Include path")
71128

72129
add_library(randomx
73130
${randomx_sources})
74-
target_link_libraries(randomx
75-
PRIVATE
76-
${CMAKE_THREAD_LIBS_INIT})
77131
set_property(TARGET randomx PROPERTY POSITION_INDEPENDENT_CODE ON)
78132
set_property(TARGET randomx PROPERTY CXX_STANDARD 11)
133+
set_property(TARGET randomx PROPERTY CXX_STANDARD_REQUIRED ON)
134+
135+
add_executable(randomx-tests
136+
src/tests/tests.cpp)
137+
target_link_libraries(randomx-tests
138+
PRIVATE randomx)
139+
set_property(TARGET randomx-tests PROPERTY POSITION_INDEPENDENT_CODE ON)
140+
set_property(TARGET randomx-tests PROPERTY CXX_STANDARD 11)
141+
142+
add_executable(randomx-codegen
143+
src/tests/code-generator.cpp)
144+
target_link_libraries(randomx-codegen
145+
PRIVATE randomx)
79146

80-
# cheat because cmake and ccache hate each other
81-
set_property(SOURCE src/jit_compiler_x86_static.S PROPERTY LANGUAGE C)
147+
set_property(TARGET randomx-codegen PROPERTY POSITION_INDEPENDENT_CODE ON)
148+
set_property(TARGET randomx-codegen PROPERTY CXX_STANDARD 11)
149+
150+
if (NOT Threads_FOUND AND UNIX AND NOT APPLE)
151+
set(THREADS_PREFER_PTHREAD_FLAG ON)
152+
find_package(Threads)
153+
endif()
154+
155+
add_executable(randomx-benchmark
156+
src/tests/benchmark.cpp
157+
src/tests/affinity.cpp)
158+
target_link_libraries(randomx-benchmark
159+
PRIVATE randomx
160+
PRIVATE ${CMAKE_THREAD_LIBS_INIT})
161+
162+
include(CheckCXXSourceCompiles)
163+
check_cxx_source_compiles("
164+
#include <cstdint>
165+
#include <atomic>
166+
int main() {
167+
std::atomic<uint64_t> a;
168+
a.is_lock_free();
169+
}" HAVE_CXX_ATOMICS)
170+
171+
if(NOT HAVE_CXX_ATOMICS)
172+
target_link_libraries(randomx-benchmark
173+
PRIVATE "atomic")
174+
endif()
175+
set_property(TARGET randomx-benchmark PROPERTY POSITION_INDEPENDENT_CODE ON)
176+
set_property(TARGET randomx-benchmark PROPERTY CXX_STANDARD 11)

README.md

+12-7
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,28 @@ Design description and analysis is available in [design.md](doc/design.md).
2020

2121
## Build
2222

23-
RandomX is written in C++11 and builds a static library with a C API provided by header file [randomx.h](src/randomx.h). Minimal API usage example is provided in [api-example1.c](src/tests/api-example1.c). The reference code includes a `benchmark` executable for testing.
23+
RandomX is written in C++11 and builds a static library with a C API provided by header file [randomx.h](src/randomx.h). Minimal API usage example is provided in [api-example1.c](src/tests/api-example1.c). The reference code includes a `randomx-benchmark` and `randomx-tests` executables for testing.
2424

2525
### Linux
2626

27-
Build dependencies: `make` and `gcc` (minimum version 4.8, but version 7+ is recommended).
27+
Build dependencies: `cmake` (minimum 2.8.7) and `gcc` (minimum version 4.8, but version 7+ is recommended).
2828

29-
Build using the provided makefile.
29+
To build optimized binaries for your machine, run:
30+
```
31+
git clone https://github.com/tevador/RandomX.git
32+
cd RandomX
33+
mkdir build && cd build
34+
cmake -DARCH=native ..
35+
make
36+
```
3037

3138
### Windows
3239

33-
Build dependencies: Visual Studio 2017.
34-
35-
A solution file is provided.
40+
On Windows, it is possible to build using MinGW (same procedure as on Linux) or using Visual Studio 2017 (solution file is provided).
3641

3742
### Precompiled binaries
3843

39-
Precompiled `benchmark` binaries are available on the [Releases page](https://github.com/tevador/RandomX/releases).
44+
Precompiled `randomx-benchmark` binaries are available on the [Releases page](https://github.com/tevador/RandomX/releases).
4045

4146
## Proof of work
4247

makefile

-200
This file was deleted.

0 commit comments

Comments
 (0)