Skip to content

Commit

Permalink
Fix compilation for multi-configuration generators and debug builds
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Snape committed Aug 23, 2016
1 parent 905fdfd commit 30d35e0
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 67 deletions.
63 changes: 23 additions & 40 deletions cmake/mex.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ function(mex_setup)

get_mex_option(CFLAGS NAMES COMPFLAGS)
get_mex_option(CXXFLAGS NAMES COMPFLAGS)
get_mex_option(DEFINES NAMES COMPDEFINES)
get_mex_option(COPTIMFLAGS NAMES OPTIMFLAGS)
get_mex_option(CXXOPTIMFLAGS NAMES OPTIMFLAGS)
get_mex_option(CDEBUGFLAGS NAMES DEBUGFLAGS)
Expand Down Expand Up @@ -197,7 +198,7 @@ function(mex_setup)
endif()

# figure out LDFLAGS for exes and shared libraries
set (MEXLIB_LDFLAGS ${MEX_LDFLAGS} ${MEX_LD_ARGUMENTS} ${MEX_CLIBS} ${MEX_LINKLIBS} ${MEX_LINKEXPORT})
set (MEXLIB_LDFLAGS ${MEX_LDFLAGS} ${MEX_LD_ARGUMENTS} ${MEX_CXXLIBS} ${MEX_LINKLIBS} ${MEX_LINKEXPORT})

if (NOT WIN32) # AND (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX))
set(MEXLIB_LDFLAGS ${MEXLIB_LDFLAGS} "-ldl")
Expand Down Expand Up @@ -225,20 +226,20 @@ function(mex_setup)


# todo: handle C separately from CXX?
set (MEX_COMPILE_FLAGS "${MEX_INCLUDE} ${MEX_CXXFLAGS} ${MEX_DEFINES} ${MEX_MATLABMEX} ${MEX_CXX_ARGUMENTS}")
string(TOUPPER "${CMAKE_BUILD_TYPE}" _build_type)
if (_build_type MATCHES DEBUG)
set(MEX_COMPILE_FLAGS "${MEX_CXXDEBUGFLAGS} ${MEX_COMPILE_FLAGS}")
if (MSVC)
string(REPLACE "/MD " "/MDd " MEX_COMPILE_FLAGS "${MEX_COMPILE_FLAGS}")
endif()
elseif (_build_type MATCHES RELEASE)
set(MEX_COMPILE_FLAGS "${MEX_CXXOPTIMFLAGS} ${MEX_COMPILE_FLAGS}")
endif()

if (${MEX_COMPILE_FLAGS} MATCHES "-ansi")
string(REPLACE "-ansi" "" MEX_COMPILE_FLAGS "${MEX_COMPILE_FLAGS}")
message(WARNING "Your MEX compiler flags contained '-ansi', but we've removed that flag for compatibility with C++11")
set (MEX_COMPILE_FLAGS "${MEX_CXXFLAGS} ${MEX_DEFINES} ${MEX_CXX_ARGUMENTS}")

# CMake will add the appropriate flags for the chosen build configuration
# instead of these flags
if(MSVC)
# Controlled by CMAKE_BUILD_TYPE
string(REPLACE "/MD " "" MEX_COMPILE_FLAGS "${MEX_COMPILE_FLAGS}")
# Controlled by CMAKE_VERBOSE_MAKEFILE
string(REPLACE "/nologo " "" MEX_COMPILE_FLAGS "${MEX_COMPILE_FLAGS}")
string(REPLACE "/nologo " "" MEXLIB_LDFLAGS "${MEXLIB_LDFLAGS}")
else()
# Controlled by CMAKE_C_STANDARD and CMAKE_CXX_STANDARD
string(REPLACE "-ansi " "" MEX_COMPILE_FLAGS "${MEX_COMPILE_FLAGS}")
string(REPLACE "-std=c++11 " "" MEX_COMPILE_FLAGS "${MEX_COMPILE_FLAGS}")
endif()

set(MEX_COMPILE_FLAGS "${MEX_COMPILE_FLAGS}" PARENT_SCOPE)
Expand All @@ -255,7 +256,7 @@ function(mex_setup)
endif()

set (MEXLIB_LDFLAGS "${MEXLIB_LDFLAGS}" PARENT_SCOPE)
# todo: add CLIBS or CXXLIBS to LINK_FLAGS selectively based in if it's a c or cxx target (always added C above)
# todo: add CLIBS or CXXLIBS to LINK_FLAGS selectively based on if it's a c or cxx target (always added CXX above)

if (MATLAB_ROOT AND MEX_EXT)
set (MATLAB_FOUND true PARENT_SCOPE)
Expand All @@ -269,7 +270,7 @@ function(mex_setup)

compare_compilers(compilers_match "${CMAKE_CXX_COMPILER}" "${MEX_CXX}")
if (NOT compilers_match)
message(WARNING "Your cmake CXX compiler is: \"${CMAKE_CXX_COMPILER}\" but your mex options end up pointing to: \"${MEX_CXX}\". You must use the same compilers. You can either:\n a) Configure the mex compiler by running 'mex -setup' in MATLAB, or \n b) Set the default compiler for cmake by setting the CC environment variable in your terminal.")
message(WARNING "Your cmake CXX compiler is: \"${CMAKE_CXX_COMPILER}\" but your mex options end up pointing to: \"${MEX_CXX}\". You must use the same compilers. You can either:\n a) Configure the mex compiler by running 'mex -setup' in MATLAB, or \n b) Set the default compiler for cmake by setting the CXX environment variable in your terminal.")
endif()

# NOTE: would like to check LD also, but it appears to be difficult with cmake (there is not explicit linker executable variable, only the make rule), and even my mex code assumes that LD==LDCXX for simplicity.
Expand Down Expand Up @@ -313,12 +314,7 @@ function(add_mex)
if (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
target_link_libraries(${target} liblast)
else()
set_target_properties(${target} PROPERTIES
LINK_FLAGS_DEBUG "${MEXLIB_LDDEBUGFLAGS} ${MEX_LDFLAGS}"
LINK_FLAGS_RELEASE "${MEXLIB_LDOPTIMFLAGS} ${MEX_LDFLAGS}"
LINK_FLAGS_MINSIZEREL "${MEXLIB_LDFLAGS} ${MEX_LDOPTIMFLAGS}"
LINK_FLAGS_RELWITHDEBINFO "${MEXLIB_LDFLAGS} ${MEX_LDDEBUGFLAGS} ${MEX_LDOPTIMFLAGS}"
)
set_target_properties(${target} PROPERTIES LINK_FLAGS "${MEXLIB_LDFLAGS}")
endif()
elseif (isshared GREATER -1)
add_library(${target} ${ARGV})
Expand All @@ -334,23 +330,15 @@ function(add_mex)
string(REPLACE "/EXPORT:mexFunction" "" __ldflags "${__ldflags}")
string(REGEX REPLACE "/implib:[^ ]+" "" __ldflags "${__ldflags}")

set_target_properties(${target} PROPERTIES
LINK_FLAGS_DEBUG "${__ldflags} ${MEX_LDDEBUGFLAGS}"
LINK_FLAGS_RELEASE "${__ldflags} ${MEX_LDOPTIMFLAGS}"
LINK_FLAGS_MINSIZEREL "${__ldflags} ${MEX_LDOPTIMFLAGS}"
LINK_FLAGS_RELWITHDEBINFO "${__ldflags} ${MEX_LDDEBUGFLAGS} ${MEX_LDOPTIMFLAGS}"
)
set_target_properties(${target} PROPERTIES LINK_FLAGS "${__ldflags}")
endif()
else ()
add_library(${target} MODULE ${ARGV})
set_target_properties(${target} PROPERTIES
COMPILE_FLAGS "-DMATLAB_MEX_FILE ${MEX_COMPILE_FLAGS}"
COMPILE_FLAGS "${MEX_COMPILE_FLAGS}"
PREFIX ""
SUFFIX ".${MEX_EXT}"
LINK_FLAGS_DEBUG "${MEXLIB_LDFLAGS} ${MEX_LDDEBUGFLAGS}"
LINK_FLAGS_RELEASE "${MEXLIB_LDFLAGS} ${MEX_LDOPTIMFLAGS}"
LINK_FLAGS_MINSIZEREL "${MEXLIB_LDFLAGS} ${MEX_LDOPTIMFLAGS}"
LINK_FLAGS_RELWITHDEBINFO "${MEXLIB_LDFLAGS} ${MEX_LDDEBUGFLAGS} ${MEX_LDOPTIMFLAGS}"
LINK_FLAGS "${MEXLIB_LDFLAGS}"
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
OUTPUT_NAME ${mexfilename}
Expand All @@ -367,12 +355,7 @@ function(add_mex)

if (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
# see comment by the definition of liblast above
set_target_properties(${target} PROPERTIES
LINK_FLAGS_DEBUG "${MEX_LDDEBUGFLAGS}"
LINK_FLAGS_RELEASE "${MEX_LDOPTIMFLAGS}"
LINK_FLAGS_MINSIZEREL "${MEX_LDOPTIMFLAGS}"
LINK_FLAGS_RELWITHDEBINFO "${MEX_LDDEBUGFLAGS} ${MEX_LDOPTIMFLAGS}"
)
set_target_properties(${target} PROPERTIES LINK_FLAGS "${MEXLIB_LDFLAGS}")
target_link_libraries(${target} liblast)
endif()
endif()
Expand Down
3 changes: 1 addition & 2 deletions cmake/pods.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,7 @@ function(pods_use_pkg_config_packages target)
# from http://stackoverflow.com/a/29871891
find_package(Threads REQUIRED)
if (THREADS_HAVE_PTHREAD_ARG)
set_property(TARGET ${target} PROPERTY COMPILE_OPTIONS "-pthread")
set_property(TARGET ${target} PROPERTY INTERFACE_COMPILE_OPTIONS "-pthread")
target_compile_options(${target} PUBLIC "-pthread")
endif()
if (CMAKE_THREAD_LIBS_INIT)
target_link_libraries(${target} "${CMAKE_THREAD_LIBS_INIT}")
Expand Down
10 changes: 5 additions & 5 deletions drake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang"
endif()
# TODO(rdeits) Remove when https://github.com/jaeandersson/swig/pull/73 is
# accessible from the version of swig_matlab we are using
set(CXX_FLAGS_NO_ERROR_MAYBE_UNINITIALIZED "-Wno-error=maybe-uninitialized")
set(CXX_FLAGS_NO_ERROR_SHADOW "-Wno-error=shadow -Wno-shadow")
set(CXX_FLAGS_NO_SIGN_COMPARE "-Wno-sign-compare")
set(CXX_FLAGS_NO_ERROR_MAYBE_UNINITIALIZED -Wno-error=maybe-uninitialized)
set(CXX_FLAGS_NO_ERROR_SHADOW -Wno-error=shadow -Wno-shadow)
set(CXX_FLAGS_NO_SIGN_COMPARE -Wno-sign-compare)
elseif(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4251")
# disable warning C4251: e.g.'RigidBody::linkname' : class 'std::basic_string<_Elem,_Traits,_Ax>' needs to have dll-interface to be used by clients of class 'RigidBody'
Expand All @@ -126,8 +126,8 @@ elseif(MSVC)

add_definitions(-D_USE_MATH_DEFINES) # to get math constants on MSVC (see https://msdn.microsoft.com/en-us/library/4hwaceh6.aspx)

set(CXX_FLAGS_NO_ERROR_SHADOW "/wd4456 /wd4457")
set(CXX_FLAGS_NO_SIGN_COMPARE "/wd4018")
set(CXX_FLAGS_NO_ERROR_SHADOW /wd4456 /wd4457)
set(CXX_FLAGS_NO_SIGN_COMPARE /wd4018)
endif()

# set up matlab build
Expand Down
9 changes: 6 additions & 3 deletions drake/bindings/swig/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ set(CMAKE_SWIG_FLAGS ${CMAKE_SWIG_FLAGS} "-w315,-401")
# Ignore SWIG errors about not being able to wrap operator <<
set(CMAKE_SWIG_FLAGS ${CMAKE_SWIG_FLAGS} "-w503")
# The autodiff.i in swig has for-loop variable warnings.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXX_FLAGS_NO_SIGN_COMPARE}")
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} APPEND PROPERTY
COMPILE_OPTIONS ${CXX_FLAGS_NO_SIGN_COMPARE})
# SWIG output code makes expansive use of variable shadowing.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXX_FLAGS_NO_ERROR_SHADOW}")
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} APPEND PROPERTY
COMPILE_OPTIONS ${CXX_FLAGS_NO_ERROR_SHADOW})
# SWIG output code "may" use a variable uninitialized (not really, but due to
# bad annotation of mexErrMsgIdAndTxt, the compiler can't know that); see also
# https://github.com/jaeandersson/swig/pull/73
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXX_FLAGS_NO_ERROR_MAYBE_UNINITIALIZED}")
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} APPEND PROPERTY
COMPILE_OPTIONS ${CXX_FLAGS_NO_ERROR_MAYBE_UNINITIALIZED})

if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# Suppresses warnings due to the existence of deprecated methods. These
Expand Down
8 changes: 4 additions & 4 deletions drake/common/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ add_executable(drake_deprecated_test drake_deprecated_test.cc)
target_link_libraries(drake_deprecated_test drakeCommon ${GTEST_BOTH_LIBRARIES})
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# Remove spurious warnings from the default build output.
set_target_properties(
target_compile_options(
drake_deprecated_test
PROPERTIES COMPILE_FLAGS "-Wno-deprecated-declarations")
PRIVATE -Wno-deprecated-declarations)
endif()
add_test(NAME drake_deprecated_test COMMAND drake_deprecated_test)

Expand All @@ -143,9 +143,9 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang"
set_target_properties(
drake_deprecated_test_nocompile
PROPERTIES EXCLUDE_FROM_ALL TRUE EXCLUDE_FROM_DEFAULT_BUILD TRUE)
set_target_properties(
target_compile_options(
drake_deprecated_test_nocompile
PROPERTIES COMPILE_FLAGS "-Werror=deprecated-declarations")
PRIVATE -Werror=deprecated-declarations)
add_test(NAME
drake_deprecated_test_nocompile
COMMAND ${CMAKE_COMMAND} --build . --target
Expand Down
9 changes: 9 additions & 0 deletions drake/solvers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ if(gurobi_FOUND)
pods_use_pkg_config_packages(drakeOptimization gurobi)
endif()

if(snopt_c_FOUND AND MATLAB_FOUND)
add_mex(NonlinearProgramSnoptmex NonlinearProgramSnoptmex.cpp)
pods_use_pkg_config_packages(NonlinearProgramSnoptmex snopt_c)

# These warnings are not worth fixing.
target_compile_options(NonlinearProgramSnoptmex PRIVATE
${CXX_FLAGS_NO_ERROR_SHADOW})
endif()

if(mosek_FOUND)
pods_use_pkg_config_packages(drakeOptimization mosek)
endif()
Expand Down
13 changes: 5 additions & 8 deletions drake/systems/plants/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ add_subdirectory(joints)

if(MATLAB_FOUND)
add_mex(rbmMexFunctions SHARED rigidBodyTreeMexFunctions.cpp)
pods_install_libraries(rbmMexFunctions)
target_link_libraries(rbmMexFunctions drakeRBM drakeMexUtil)
# These warnings are not worth fixing.
set_target_properties(
rbmMexFunctions PROPERTIES COMPILE_FLAGS ${CXX_FLAGS_NO_SIGN_COMPARE})
target_compile_options(rbmMexFunctions PRIVATE ${CXX_FLAGS_NO_SIGN_COMPARE})

macro(add_rbm_mex)
# TODO: use this again once I can assume everyone has CMAKE version >= 2.8.8
Expand All @@ -68,8 +68,7 @@ if(MATLAB_FOUND)
add_mex(${ARGV} ${ARGV}.cpp)
target_link_libraries(${ARGV} rbmMexFunctions)
# These warnings are not worth fixing.
set_target_properties(
${ARGV} PROPERTIES COMPILE_FLAGS ${CXX_FLAGS_NO_SIGN_COMPARE})
target_compile_options(${ARGV} PRIVATE ${CXX_FLAGS_NO_SIGN_COMPARE})
endmacro()

add_rbm_mex(constructModelmex)
Expand Down Expand Up @@ -122,8 +121,7 @@ if(MATLAB_FOUND)
add_mex(${ARGV} ${ARGV}.cpp)
target_link_libraries(${ARGV} drakeRBM drakeMexUtil drakeIKoptions)
# These warnings are not worth fixing.
set_target_properties(
${ARGV} PROPERTIES COMPILE_FLAGS ${CXX_FLAGS_NO_SIGN_COMPARE})
target_compile_options(${ARGV} PRIVATE ${CXX_FLAGS_NO_SIGN_COMPARE})
endmacro()
add_ikoptions_mex(IKoptionsmex)

Expand Down Expand Up @@ -167,8 +165,7 @@ if(MATLAB_FOUND)
add_mex(${ARGV} ${ARGV}.cpp)
target_link_libraries(${ARGV} drakeIK drakeRBM drakeMexUtil drakeRigidBodyConstraint)
# These warnings are not worth fixing.
set_target_properties(
${ARGV} PROPERTIES COMPILE_FLAGS ${CXX_FLAGS_NO_SIGN_COMPARE})
target_compile_options(${ARGV} PRIVATE ${CXX_FLAGS_NO_SIGN_COMPARE})
endmacro()

if(snopt_c_FOUND)
Expand Down
4 changes: 2 additions & 2 deletions drake/systems/plants/constraint/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ macro(add_constraint_mex)
add_mex(${ARGV} ${ARGV}.cpp)
target_link_libraries(${ARGV} drakeRBM drakeMexUtil drakeRigidBodyConstraint drakeConstructRigidBodyConstraint)
# These warnings are not worth fixing.
set_target_properties(
${ARGV} PROPERTIES COMPILE_FLAGS ${CXX_FLAGS_NO_SIGN_COMPARE})
target_compile_options(${ARGV} PRIVATE ${CXX_FLAGS_NO_SIGN_COMPARE})
endmacro()

add_library_with_exports(LIB_NAME drakeRigidBodyConstraint SOURCE_FILES RigidBodyConstraint.cpp)
Expand All @@ -18,6 +17,7 @@ pods_install_pkg_config_file(drake-rigid-body-constraint
if(MATLAB_FOUND)
add_mex(drakeConstructRigidBodyConstraint SHARED constructPtrRigidBodyConstraint.cpp)
target_link_libraries(drakeConstructRigidBodyConstraint drakeRigidBodyConstraint drakeRBM drakeMexUtil drakeGeometryUtil)
pods_install_libraries(drakeConstructRigidBodyConstraint)
add_constraint_mex(testSingleTimeKinCnstmex)
add_constraint_mex(testMultipleTimeKinCnstmex)
add_constraint_mex(testQuasiStaticConstraintmex)
Expand Down
5 changes: 2 additions & 3 deletions drake/systems/trajectories/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ if(BLAS_LIBRARY AND snopt_c_FOUND AND bullet_FOUND AND MATLAB_FOUND)
target_link_libraries(ptToPolyBullet_mex drakeMexUtil)

# These warnings are not worth fixing.
set_target_properties(
replanFunnels_mex shiftFunnel_snopt_mex
PROPERTIES COMPILE_FLAGS ${CXX_FLAGS_NO_ERROR_SHADOW})
target_compile_options(replanFunnels_mex PRIVATE ${CXX_FLAGS_NO_ERROR_SHADOW})
target_compile_options(shiftFunnel_snopt_mex PRIVATE ${CXX_FLAGS_NO_ERROR_SHADOW})
endif()

add_subdirectory(test)

0 comments on commit 30d35e0

Please sign in to comment.