aboutsummaryrefslogtreecommitdiffstats
path: root/CMakeLists.txt
blob: 67b0c18b85ca5c09531c2681f29d4d8f6cf5aadf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# TODO: sensible minimum CMake version
cmake_minimum_required(VERSION 3.3)
project(nextpnr)

option(BUILD_GUI "Build GUI" ON)
option(BUILD_PYTHON "Build Python Integration" ON)
option(BUILD_TESTS "Build GUI" OFF)
option(BUILD_HEAP "Build HeAP analytic placer" ON)
option(USE_OPENMP "Use OpenMP to accelerate analytic placer" OFF)
option(COVERAGE "Add code coverage info" OFF)
option(STATIC_BUILD "Create static build" OFF)
option(EXTERNAL_CHIPDB "Create build with pre-built chipdb binaries" OFF)
option(SERIALIZE_CHIPDB "Never build chipdb in parallel to reduce peak memory use" ON)

set(link_param "")
if (STATIC_BUILD)
    set(Boost_USE_STATIC_LIBS   ON)
    if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
    elseif(${CMAKE_SYSTEM_NAME} MATCHES "Windows" AND MSVC)
        set(CMAKE_CXX_FLAGS_RELEASE "/MT")
        set(CMAKE_CXX_FLAGS_DEBUG "/MTd")
        if (BUILD_PYTHON)
            add_definitions(-DBOOST_PYTHON_STATIC_LIB)
        endif()
    else()
        set(CMAKE_FIND_LIBRARY_SUFFIXES ".a" ".so")
        set(link_param "-static")
        if (BUILD_PYTHON)
            find_package(ZLIB)
            find_package(EXPAT)
            find_package(Threads)
        endif()
    endif()
endif()

if (EXTERNAL_CHIPDB)
    if (NOT DEFINED EXTERNAL_CHIPDB_ROOT)
        message(STATUS "EXTERNAL_CHIPDB_ROOT not defined using -DEXTERNAL_CHIPDB_ROOT=/path/to/nextpnr. Default to /usr/share/nextpnr")
        set(EXTERNAL_CHIPDB_ROOT "/usr/share/nextpnr")
    endif()
    add_definitions("-DEXTERNAL_CHIPDB_ROOT=\"${EXTERNAL_CHIPDB_ROOT}\"")
endif()

# List of families to build
set(FAMILIES generic ice40 ecp5)

set(ARCH "" CACHE STRING "Architecture family for nextpnr build")
set_property(CACHE ARCH PROPERTY STRINGS ${FAMILIES})

if (NOT ARCH)
    message(STATUS "Architecture needs to be set, set desired one with -DARCH=xxx")
    message(STATUS "Supported architectures are :")
    message(STATUS "  all")
    foreach(item ${FAMILIES})
        message(STATUS "  ${item}")
    endforeach()
    message(FATAL_ERROR "Architecture setting is mandatory")
endif ()

if (ARCH STREQUAL "all")
    SET(ARCH ${FAMILIES})
endif()

foreach(item ${ARCH})
    if (NOT item IN_LIST FAMILIES)
        message(FATAL_ERROR "Architecture '${item}' not in list of supported architectures")
    endif()
endforeach()

set(CMAKE_CXX_STANDARD 11)
if (MSVC)
    set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "" FORCE)
    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /D_DEBUG /W4 /wd4100 /wd4244 /wd4125 /wd4800 /wd4456 /wd4458 /wd4305 /wd4459 /wd4121 /wd4996")
    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /W4 /wd4100 /wd4244 /wd4125 /wd4800 /wd4456 /wd4458 /wd4305 /wd4459 /wd4121 /wd4996 /wd4127")
else()
    set(CMAKE_CXX_FLAGS_DEBUG "-Wall -fPIC -ggdb -pipe")
    if (USE_OPENMP)
        set(CMAKE_CXX_FLAGS_RELEASE "-Wall -fPIC -O3 -g -pipe -fopenmp")
    else()
        set(CMAKE_CXX_FLAGS_RELEASE "-Wall -fPIC -O3 -g -pipe")
    endif()
endif()
set(CMAKE_DEFIN)

set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/3rdparty/sanitizers-cmake/cmake;." ${CMAKE_MODULE_PATH})

if (COVERAGE)
    include(CodeCoverage)
endif()

if(NOT DEFINED CMAKE_SUPPRESS_DEVELOPER_WARNINGS)
     set(CMAKE_SUPPRESS_DEVELOPER_WARNINGS 1 CACHE INTERNAL "No dev warnings")
endif()

find_package(Sanitizers)

# List of Boost libraries to include
set(boost_libs filesystem thread program_options iostreams)

if (BUILD_GUI AND NOT BUILD_PYTHON)
    message(FATAL_ERROR "GUI requires Python to build")
endif()

find_package(PythonInterp 3.5 REQUIRED)
if (BUILD_PYTHON)
    # TODO: sensible minimum Python version
    find_package(PythonLibs 3.5 REQUIRED)
else()
    add_definitions("-DNO_PYTHON")
endif()

find_package(Boost REQUIRED COMPONENTS ${boost_libs})

if (BUILD_GUI)
    # Find the Qt5 libraries
    find_package(Qt5 COMPONENTS Core Widgets OpenGL REQUIRED)
    find_package(OpenGL REQUIRED)
else()
    add_definitions("-DNO_GUI")
endif()

# Get the latest abbreviated commit hash of the working branch
execute_process(
  COMMAND git log -1 --format=%h
  WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  OUTPUT_VARIABLE GIT_COMMIT_HASH
  OUTPUT_STRIP_TRAILING_WHITESPACE
)

if (BUILD_TESTS)
    add_subdirectory(3rdparty/googletest/googletest ${CMAKE_CURRENT_BINARY_DIR}/generated/3rdparty/googletest EXCLUDE_FROM_ALL)
    enable_testing()
endif()

if (BUILD_GUI)
    add_subdirectory(3rdparty/QtPropertyBrowser ${CMAKE_CURRENT_BINARY_DIR}/generated/3rdparty/QtPropertyBrowser EXCLUDE_FROM_ALL)
endif()

add_definitions("-DGIT_COMMIT_HASH=${GIT_COMMIT_HASH}")

configure_file(
    ${CMAKE_CURRENT_SOURCE_DIR}/common/version.h.in ${CMAKE_CURRENT_BINARY_DIR}/generated/version.h
)

if (BUILD_PYTHON)
    # Find Boost::Python of a suitable version in a cross-platform way
    # Some distributions (Arch) call it libboost_python3, others such as Ubuntu
    # call it libboost_python35. In the latter case we must consider all minor versions
    # Original source: https://github.com/BVLC/caffe/blob/master/cmake/Dependencies.cmake#L148
    set(version ${PYTHONLIBS_VERSION_STRING})

    STRING(REGEX REPLACE "[^0-9]" "" boost_py_version "${version}")
    find_package(Boost QUIET COMPONENTS "python-py${boost_py_version}" ${boost_libs})
    set(Boost_PYTHON_FOUND ${Boost_PYTHON-PY${boost_py_version}_FOUND})

    while (NOT "${version}" STREQUAL "" AND NOT Boost_PYTHON_FOUND)
        STRING(REGEX REPLACE "([0-9.]+).[0-9]+" "\\1" version "${version}")

        STRING(REGEX REPLACE "[^0-9]" "" boost_py_version "${version}")
        find_package(Boost QUIET COMPONENTS "python-py${boost_py_version}" ${boost_libs})
        set(Boost_PYTHON_FOUND ${Boost_PYTHON-PY${boost_py_version}_FOUND})

        STRING(REGEX MATCHALL "([0-9.]+).[0-9]+" has_more_version "${version}")
        if ("${has_more_version}" STREQUAL "")
            break()
        endif ()
    endwhile ()

    if (NOT Boost_PYTHON_FOUND)
        foreach (PyVer 3 36 37 38)
            find_package(Boost QUIET COMPONENTS python${PyVer} ${boost_libs})
            if ("${Boost_LIBRARIES}" MATCHES ".*(python|PYTHON).*" )
                set(Boost_PYTHON_FOUND TRUE)
                break()
            endif ()
        endforeach ()
    endif ()

    if (NOT Boost_PYTHON_FOUND)
        STRING(REGEX REPLACE "([0-9]+\\.[0-9]+).*" "\\1" gentoo_version "${PYTHONLIBS_VERSION_STRING}")
        find_package(Boost QUIET COMPONENTS python-${gentoo_version} ${boost_libs})
        if ("${Boost_LIBRARIES}" MATCHES ".*(python|PYTHON).*" )
            set(Boost_PYTHON_FOUND TRUE)
        endif ()
    endif ()

    if (NOT Boost_PYTHON_FOUND )
        message( FATAL_ERROR "No version of Boost::Python 3.x could be found.")
    endif ()
endif()

include_directories(common/ json/ ${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS})

if(BUILD_HEAP)
    find_package (Eigen3 REQUIRED NO_MODULE)
    include_directories(${EIGEN3_INCLUDE_DIRS})
    add_definitions(${EIGEN3_DEFINITIONS})
    add_definitions(-DWITH_HEAP)
endif()

aux_source_directory(common/ COMMON_SRC_FILES)
aux_source_directory(json/ JSON_PARSER_FILES)
set(COMMON_FILES ${COMMON_SRC_FILES} ${JSON_PARSER_FILES})
set(CMAKE_BUILD_TYPE Release)

if(MINGW)
    add_definitions("-Wa,-mbig-obj")
endif(MINGW)

include(bba/bba.cmake)

foreach (family ${ARCH})
    message(STATUS "Configuring architecture : ${family}")
    string(TOUPPER ${family} ufamily)
    aux_source_directory(${family}/ ${ufamily}_FILES)

    if (BUILD_GUI)
       add_subdirectory(gui ${CMAKE_CURRENT_BINARY_DIR}/generated/gui/${family} EXCLUDE_FROM_ALL)
    endif()

    # Add the CLI binary target
    add_executable(nextpnr-${family} ${COMMON_FILES} ${${ufamily}_FILES})
    install(TARGETS nextpnr-${family} RUNTIME DESTINATION bin)
    target_compile_definitions(nextpnr-${family} PRIVATE MAIN_EXECUTABLE)

    # Add any new per-architecture targets here
    if (BUILD_TESTS)
        if (COVERAGE)
            APPEND_COVERAGE_COMPILER_FLAGS()
            set(COVERAGE_LCOV_EXCLUDES '/usr/include/*' '3rdparty/*' 'generated/*' 'bba/*' 'tests/*')
            SETUP_TARGET_FOR_COVERAGE_LCOV(
                NAME ${family}-coverage
                EXECUTABLE nextpnr-${family}-test
                DEPENDENCIES nextpnr-${family}-test
            )
        endif()

        aux_source_directory(tests/${family}/ ${ufamily}_TEST_FILES)
        if (BUILD_GUI)
            aux_source_directory(tests/gui/ GUI_TEST_FILES)
        endif()

        add_executable(nextpnr-${family}-test ${${ufamily}_TEST_FILES}
                ${COMMON_FILES} ${${ufamily}_FILES} ${GUI_TEST_FILES})
        target_link_libraries(nextpnr-${family}-test PRIVATE gtest_main)
        add_sanitizers(nextpnr-${family}-test)

        add_test(${family}-test ${CMAKE_CURRENT_BINARY_DIR}/nextpnr-${family}-test)
    endif()

    # Set ${family_targets} to the list of targets being build for this family
    set(family_targets nextpnr-${family})

    if (BUILD_TESTS)
        set(family_targets ${family_targets} nextpnr-${family}-test)
    endif()

    # Include the family-specific CMakeFile
    include(${family}/family.cmake)
    foreach (target ${family_targets})
        # Include family-specific source files to all family targets and set defines appropriately
        target_include_directories(${target} PRIVATE ${family}/ ${CMAKE_CURRENT_BINARY_DIR}/generated/)
        target_compile_definitions(${target} PRIVATE NEXTPNR_NAMESPACE=nextpnr_${family} ARCH_${ufamily} ARCHNAME=${family})
        target_link_libraries(${target} LINK_PUBLIC ${Boost_LIBRARIES} ${link_param})
        if (NOT MSVC)
            target_link_libraries(${target} LINK_PUBLIC pthread)
        endif()
        add_sanitizers(${target})
        if (BUILD_GUI)
            target_include_directories(${target} PRIVATE gui/${family}/ gui/)
            target_compile_definitions(${target} PRIVATE QT_NO_KEYWORDS)
            target_link_libraries(${target} LINK_PUBLIC gui_${family} ${GUI_LIBRARY_FILES_${ufamily}})
        endif()
        if (BUILD_PYTHON)
            target_link_libraries(${target} LINK_PUBLIC ${PYTHON_LIBRARIES})
            if (STATIC_BUILD)
                target_link_libraries(${target} LINK_PUBLIC ${CMAKE_THREAD_LIBS_INIT} ${CMAKE_DL_LIBS} ${ZLIB_LIBRARIES} ${EXPAT_LIBRARIES} -lutil)
                if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
                elseif(${CMAKE_SYSTEM_NAME} MATCHES "Windows" AND MSVC)
                else()
                    target_link_libraries(${target} LINK_PUBLIC -lutil)
                endif()
            endif()
        endif()
    endforeach (target)
endforeach (family)

file(GLOB_RECURSE CLANGFORMAT_FILES *.cc *.h)
string(REGEX REPLACE "[^;]*/ice40/chipdbs/chipdb-[^;]*.cc" "" CLANGFORMAT_FILES "${CLANGFORMAT_FILES}")
string(REGEX REPLACE "[^;]*/ecp5/chipdbs/chipdb-[^;]*.cc" "" CLANGFORMAT_FILES "${CLANGFORMAT_FILES}")
string(REGEX REPLACE "[^;]*/3rdparty[^;]*" "" CLANGFORMAT_FILES "${CLANGFORMAT_FILES}")
string(REGEX REPLACE "[^;]*/generated[^;]*" "" CLANGFORMAT_FILES "${CLANGFORMAT_FILES}")

add_custom_target(
    clangformat
    COMMAND clang-format
    -style=file
    -i
    ${CLANGFORMAT_FILES}
)