Skip to content

Commit

Permalink
refactor codebase to c++
Browse files Browse the repository at this point in the history
  • Loading branch information
ahoopes committed Feb 27, 2019
1 parent 34b3383 commit b156d5a
Show file tree
Hide file tree
Showing 1,851 changed files with 508,611 additions and 674,275 deletions.
211 changes: 78 additions & 133 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# before starting the project, set gcc/g++ as default
# before starting the project, set gcc/g++ as the default compiler
find_program(CMAKE_C_COMPILER NAMES $ENV{CC} gcc PATHS ENV PATH NO_DEFAULT_PATH)
find_program(CMAKE_CXX_COMPILER NAMES $ENV{CXX} g++ PATHS ENV PATH NO_DEFAULT_PATH)

Expand All @@ -9,9 +9,10 @@ enable_language(C CXX Fortran)

# a few build options
option(MINIMAL "Only build core components" OFF)
option(BUILD_GUIS "Build GUIs" ON)
option(BUILD_GUIS "Build GUI tools" ON)
option(USE_OPENCL "Build OpenCL algorithms" OFF)
option(INFANT_MODULE "Include infant recon-all" OFF)
option(SUPPRESS_WARNINGS "Suppress some selected warnings" ON)

if(NOT APPLE)
# linux-only build options
Expand All @@ -26,17 +27,11 @@ include(cmake/functions.cmake)
include(TestBigEndian)
include(CheckFunctionExists)

# prevents itk from importing as a system
# prevent third-party packages from importing as a system
set(CMAKE_NO_SYSTEM_FROM_IMPORTED TRUE)

# xxd is used to generate the helptext headers - make sure it exists
find_program(XXD xxd)
if(NOT XXD)
message(FATAL_ERROR "The xxd program is required to build freesurfer")
endif()

# allow reading installation dir from environment var, but if not defined,
# then override default install dir /usr/local (too messy to install there)
# if an install prefix is not provided, check the FS_INSTALL_DIR env var, and
# if that is not defined, set the default path to /usr/local/freesurfer
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
if(NOT "$ENV{FS_INSTALL_DIR}" STREQUAL "")
set(CMAKE_INSTALL_PREFIX "$ENV{FS_INSTALL_DIR}" CACHE PATH "Copied from FS_INSTALL_DIR env variable" FORCE)
Expand All @@ -59,11 +54,16 @@ if(NOT FS_BUILD_STAMP)
set(FS_BUILD_STAMP "freesurfer")
endif()
install(CODE "file(WRITE ${CMAKE_INSTALL_PREFIX}/build-stamp.txt ${FS_BUILD_STAMP}\\n)")

# --------------------------------------------------
# library dependencies and third-party packages
# external dependencies
# --------------------------------------------------

# xxd is used to generate the helptext headers
find_program(XXD xxd)
if(NOT XXD)
message(FATAL_ERROR "The xxd program is required to build freesurfer")
endif()

# Most of the packages required by freesurfer are located by custom find-modules stored in the
# 'cmake' subdir. The find-modules expect each package to be installed under a common
# path defined by FS_PACKAGES_DIR. On Martinos machines, this variable automatically defaults
Expand Down Expand Up @@ -104,17 +104,13 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
find_package(ZLIB REQUIRED)
add_definitions(-DHAVE_ZLIB)

# -------- gfortran/blas/lapack --------
if(NOT APPLE)

# -------- gfortran --------
find_library(GFORTRAN_LIBRARIES
HINTS /usr/lib/gcc/x86_64-linux-gnu/4.8 /usr/lib/gcc/x86_64-redhat-linux/4.8.2/
NAMES libgfortran.a gfortran)

# -------- blas and lapack --------
find_library(BLAS_LIBRARIES NAMES libblas.a)
find_library(LAPACK_LIBRARIES NAMES liblapack.a)

endif()

# -------- armadillo --------
Expand Down Expand Up @@ -196,7 +192,7 @@ if(VTK_FOUND AND NOT APPLE)
endif()

# --------------------------------------------------
# global build configuration
# global system information
# --------------------------------------------------

add_definitions(-D${CMAKE_SYSTEM_NAME})
Expand All @@ -209,89 +205,20 @@ else()
set(BYTEORDER 1234)
endif()

# prevents itk from importing as a system

# to see all warnings add -DSUPPRESS_WARNINGS=OFF to cmake command line
option(SUPPRESS_WARNINGS "Allow building in subdirs to supress warnings" ON)
# option(SUPPRESS_WARNINGS "Allow building in subdirs to supress warnings" OFF)

# compiler warnings
set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wall")
# clang complains about -Wno-unused-but-set-variable and says to use -Wno-unused-const-variable

if(SUPPRESS_WARNINGS)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wno-unused-const-variable")
else()
set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wno-unused-but-set-variable")
endif()
endif()

# ANSI
add_definitions(-DANSI)

# SSE matrix and math functions (affine.h and sse_mathfun.h)
# todo: write some logic to set this correctly
add_definitions(-DUSE_SSE_MATHFUN)
set(C_CXX_FLAGS "${C_CXX_FLAGS} -msse2 -mfpmath=sse -fPIC")

# large file support: support for files > 2GB
if(APPLE)
add_definitions(-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE)
endif()

# todo: implement processor-based logic to determine this
set(C_CXX_FLAGS "${C_CXX_FLAGS} -m64")

# for stripping unused code
if(APPLE)
set(STRIP_FLAGS "-dead_strip")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${STRIP_FLAGS} -static-libgcc")
else()
if(SUPPRESS_WARNINGS)
set(STRIP_FLAGS "-fdata-sections -ffunction-sections -Wl,--gc-sections")
else()
# set(STRIP_FLAGS "-fdata-sections -ffunction-sections")
# Need this linker option for linux else, e.g., linking of gifti_test fails
set(STRIP_FLAGS "-fdata-sections -ffunction-sections -Wl,--gc-sections")

endif()
set(C_CXX_FLAGS "${C_CXX_FLAGS} ${STRIP_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${STRIP_FLAGS}")
endif()

if(NOT APPLE)
# link map (only for linux)
if(SUPPRESS_WARNINGS)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Map,ld_map.txt -Wl,--no-demangle")
endif()
endif()

# check for clock_gettime on mac
if(APPLE)
check_function_exists(clock_gettime HAVE_CLOCK_GETTIME)
if(HAVE_CLOCK_GETTIME)
add_definitions(-DHAVE_CLOCK_GETTIME)
endif()
endif()

# apply C and CXX flags
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${C_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${C_CXX_FLAGS}")

# enable std c++11
set(CMAKE_CXX_STANDARD 11)

# set executable rpath
if(NOT APPLE)
set(CMAKE_INSTALL_RPATH "$ORIGIN/../lib:${CMAKE_INSTALL_RPATH}")
endif()
# --------------------------------------------------
# third-party code
# --------------------------------------------------

add_subdirectory(packages)

# --------------------------------------------------
# setup python
# setup python
# --------------------------------------------------

# Unfortunately, the python version used to run bind c-libraries must be equivalent to
# Unfortunately, the python version used to run pybind c-libraries must be equivalent to
# the version used to build the libraries. The easiest and least intrusive way of making freesurfer
# python scripts run out-of-the-box (and to help guarantee reproducibility) requires
# distributing a minimal, custom python installation called fspython. This fspython package
Expand All @@ -316,58 +243,76 @@ if(DISTRIBUTE_FSPYTHON)
endif()

# initialize pybind for python wrapping
set(PYBIND11_PYTHON_VERSION 3.5) # minimum version required
set(PYBIND11_PYTHON_VERSION 3.5)
add_subdirectory(packages/pybind11)

if(NOT DISTRIBUTE_FSPYTHON)
# link to an external python binary if fspython won't be distributed
symlink(${PYTHON_EXECUTABLE} ${CMAKE_INSTALL_PREFIX}/python/bin/python3)
endif()

# --------------------------------------------------
# freesurfer build settings
# --------------------------------------------------

# warnings
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wno-sign-compare -Wno-write-strings")

# clang complains about -Wno-unused-but-set-variable and says to use -Wno-unused-const-variable
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-const-variable")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-but-set-variable")
endif()

# ANSI
add_definitions(-DANSI)

# SSE matrix and math functions (affine.h and sse_mathfun.h)
# todo: write some logic to set this correctly
add_definitions(-DUSE_SSE_MATHFUN)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64 -msse2 -mfpmath=sse -fPIC")

# large file support: support for files > 2GB
# todo: can this just be removed entirely?
if(APPLE)
add_definitions(-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE)
endif()

# linker options
if(APPLE)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -dead_strip -static-libgcc")
else()
set(STRIP_FLAGS "-fdata-sections -ffunction-sections -Wl,--gc-sections")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${STRIP_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${STRIP_FLAGS} -Wl,-Map,ld_map.txt -Wl,--no-demangle")
endif()

# check for clock_gettime on mac
if(APPLE)
check_function_exists(clock_gettime HAVE_CLOCK_GETTIME)
if(HAVE_CLOCK_GETTIME)
add_definitions(-DHAVE_CLOCK_GETTIME)
endif()
endif()

# --------------------------------------------------
# build freesurfer
# --------------------------------------------------

# first, prepare the freesurfer distribution
# prepare the freesurfer distribution
add_subdirectory(distribution)

# the top-level include dir contain the most commonly
# included freesurfer header files
# the top-level include dir contain the most commonly included freesurfer header files
set(FS_INCLUDE_DIRS
${CMAKE_SOURCE_DIR}
${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/packages/minc
${CMAKE_SOURCE_DIR}/packages/netcdf
${CMAKE_SOURCE_DIR}/packages/nifti
)

# build third-party libraries
# make sure to use the local minc source
add_definitions(-DUSE_LOCAL_MINC)
add_subdirectories(
packages/jpeg
packages/expat
packages/tiff
packages/xml2
packages/minc
packages/netcdf
packages/tetgen
)

if(OPENGL_FOUND)
add_subdirectory(packages/glut)
endif()

# now build the freesurfer static libraries (order here matters!)
add_subdirectories(
log
rgb
hipsstubs
dicom
utils
utilscpp
svm-lib
opencl_algorithms
)
# build the freesurfer static libraries
add_subdirectories(utils opencl_algorithms)

# the following utility libraries are required for freeview
add_subdirectories(vtkutils lineprof)
Expand Down Expand Up @@ -476,9 +421,8 @@ add_subdirectories(
tkregister2
)

# the following program subdirectories aren't required in the standard
# recon-all stream - they will be built by default, but not if a minimal build
# is configured
# the following program subdirectories aren't required in the standard recon-all stream.
# they will be built by default, but not if a minimal build is configured
if(NOT MINIMAL)
add_subdirectories(
anatomicuts
Expand All @@ -487,6 +431,7 @@ if(NOT MINIMAL)
connectgraph
diffusion_tool
dmri_tensoreig
dummy
dngtester
freeview
fsfast
Expand Down Expand Up @@ -622,7 +567,7 @@ if(NOT MINIMAL)
mri_update_gca
mri_vol2roi
mri_volcluster
mri_volsynth
# mri_volsynth
mri_warp_convert
mri_wbc
mri_wmfilter
Expand Down Expand Up @@ -683,7 +628,7 @@ if(NOT MINIMAL)
mris_multiscale_stats
mris_niters2fwhm
mris_parcellate_connectivity
mris_pmake
# mris_pmake
mris_register_label_map
mris_register_to_volume
mris_remove_variance
Expand Down
6 changes: 3 additions & 3 deletions anatomicuts/AnatomiCuts_correspondences.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
#include <string>
#include "vtkSplineFilter.h"
#include "OrientationPlanesFromParcellationFilter.h"
extern "C"
{


#include "colortab.h"
#include "fsenv.h"
}


typedef std::vector<int> PointDataType;
typedef float PixelType;
Expand Down
5 changes: 1 addition & 4 deletions anatomicuts/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ if(VTK_FOUND)

include(${ITK_USE_FILE})
include(${VTK_USE_FILE})


if(SUPPRESS_WARNINGS)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated -Wmaybe-uninitialized")
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated -Wno-maybe-uninitialized -Wno-unused-variable -Wno-unused-local-typedefs")

set(TRACKIO ../freeview/track_io/TrackIO.cpp)

Expand Down
2 changes: 1 addition & 1 deletion check_siemens_dir/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ project(check_siemens_dir)

include_directories(${FS_INCLUDE_DIRS})

add_executable(check_siemens_dir check_siemens_dir.c)
add_executable(check_siemens_dir check_siemens_dir.cpp)
target_link_libraries(check_siemens_dir utils)

install(TARGETS check_siemens_dir DESTINATION bin)
Loading

0 comments on commit b156d5a

Please sign in to comment.