From 78236915c7882e41d94704e745e1d35345426fa6 Mon Sep 17 00:00:00 2001 From: Kasper Marstal <kaspermarstal@gmail.com> Date: Mon, 9 Nov 2015 06:46:34 -0800 Subject: [PATCH] COMP: Add minimum viable linking error example in Testing/Unit/CMakeLists.txt --- CMake/elxComponent.cmake | 10 ---- CMake/elxWinConfig.cmake | 37 +++++------- CMake/elxWinConfigGoogleTest.cmake | 6 -- CMakeLists.txt | 15 ++--- SuperBuild/CMakeLists.txt | 2 + SuperBuild/ExternalBoost.cmake | 3 +- Testing/CMakeLists.txt | 4 -- Testing/Unit/CMakeLists.txt | 25 ++++++-- Testing/Unit/HelloWorld.cxx | 7 +++ Testing/Unit/HelloWorldBlueprint.cxx | 12 ++++ Testing/Unit/HelloWorldGoogleTest.cxx | 7 +++ Testing/Unit/itkRegistration.cxx | 86 --------------------------- 12 files changed, 70 insertions(+), 144 deletions(-) delete mode 100644 CMake/elxComponent.cmake delete mode 100644 CMake/elxWinConfigGoogleTest.cmake create mode 100644 Testing/Unit/HelloWorld.cxx create mode 100644 Testing/Unit/HelloWorldBlueprint.cxx create mode 100644 Testing/Unit/HelloWorldGoogleTest.cxx delete mode 100644 Testing/Unit/itkRegistration.cxx diff --git a/CMake/elxComponent.cmake b/CMake/elxComponent.cmake deleted file mode 100644 index 4fd04dfb..00000000 --- a/CMake/elxComponent.cmake +++ /dev/null @@ -1,10 +0,0 @@ -macro( ElastixComponent COMPONENT ) - ElastixModuleCheckName( ${COMPONENT} ) - set( ELASTIX_COMPONENT_{$COMPONENT}_DEFINED TRUE ) -endmacro() - -macro( ElastixComponentCheckName COMPONENT ) - if( NOT "${_name}" MATCHES "^[a-zA-Z][a-zA-Z0-9]*$" ) - message( FATAL_ERROR "Invalid component name: ${COMPONENT}" ) - endif() -endmacro() \ No newline at end of file diff --git a/CMake/elxWinConfig.cmake b/CMake/elxWinConfig.cmake index 6f83da4f..e0b1b38c 100644 --- a/CMake/elxWinConfig.cmake +++ b/CMake/elxWinConfig.cmake @@ -1,25 +1,18 @@ # Visual Studio complains if paths are too long +string( LENGTH "${CMAKE_CURRENT_SOURCE_DIR}" n ) +if( n GREATER 50 ) +message( + FATAL_ERROR + "Source code directory path length is too long for MSVC (${n} > 50)." + "Please move the source code directory to a directory with a shorter path." + ) +endif() -if( MSVC ) - string( LENGTH "${CMAKE_CURRENT_SOURCE_DIR}" n ) - if( n GREATER 50 ) - message( - FATAL_ERROR - "Source code directory path length is too long for MSVC (${n} > 50)." - "Please move the source code directory to a directory with a shorter path." - ) - endif() - - string( LENGTH "${CMAKE_CURRENT_BINARY_DIR}" n ) - if( n GREATER 50 ) - message( - FATAL_ERROR - "Build directory path length is too long for MSVC (${n} > 50)." - "Please move the build directory to a directory with a shorter path." - ) - endif() - - set( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /bigobj" ) - set( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /bigobj" ) - set( CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /bigobj" ) +string( LENGTH "${CMAKE_CURRENT_BINARY_DIR}" n ) +if( n GREATER 50 ) +message( + FATAL_ERROR + "Build directory path length is too long for MSVC (${n} > 50)." + "Please move the build directory to a directory with a shorter path." + ) endif() \ No newline at end of file diff --git a/CMake/elxWinConfigGoogleTest.cmake b/CMake/elxWinConfigGoogleTest.cmake deleted file mode 100644 index 75a5ac06..00000000 --- a/CMake/elxWinConfigGoogleTest.cmake +++ /dev/null @@ -1,6 +0,0 @@ -# GoogleTest needs static linking - -include( elxCompilerFlags.cmake ) -foreach( CompilerFlag ${CompilerFlags} ) - string( REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}" ) -endforeach() diff --git a/CMakeLists.txt b/CMakeLists.txt index 0d48eb59..81e9fb94 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required( VERSION 2.8 ) # Explicitly add INCREMENTAL linking option to command lines. # http://www.cmake.org/pipermail/cmake/2010-February/035174.html -set( MSVC_INCREMENTAL_DEFAULT ON ) +#set( MSVC_INCREMENTAL_DEFAULT ON ) # --------------------------------------------------------------------- project( Elastix ) @@ -13,13 +13,10 @@ set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ) # Include SuperElastix CMake scripts -set( CMAKE_MODULE_PATH - "${CMAKE_CURRENT_SOURCE_DIR}/CMake" - ${CMAKE_MODULE_PATH} -) +list( APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMake" ) -if( ${CMAKE_CXX_COMPILER_ID} STREQUAL "MSVC" ) - include( elxWinConfig.cmake ) +if( ${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC ) + include( elxWinConfig ) endif() # --------------------------------------------------------------------- @@ -31,13 +28,13 @@ include( "${CMAKE_CURRENT_SOURCE_DIR}/CMake/elxITKRequiredModules.cmake" ) # --------------------------------------------------------------------- # Boost Graph Library -find_package( Boost REQUIRED graph ) +find_package( Boost ) include_directories( ${Boost_INCLUDE_DIRS} ) # --------------------------------------------------------------------- # Build Elastix # For now we just enable all modules -include( "${CMAKE_CURRENT_SOURCE_DIR}/CMake/elxModules.cmake" ) +include( elxModules ) elxmodule_enable( elxModuleCore ) # --------------------------------------------------------------------- diff --git a/SuperBuild/CMakeLists.txt b/SuperBuild/CMakeLists.txt index b9f53783..0043bd71 100644 --- a/SuperBuild/CMakeLists.txt +++ b/SuperBuild/CMakeLists.txt @@ -23,6 +23,8 @@ if( NOT DEFINED CMAKE_BUILD_TYPE ) set( CMAKE_BUILD_TYPE "Release" ) endif() +option( BUILD_SHARED_LIBS OFF ) + # Build examples by default # Examples will be build as an external project to verify the installation of elastix option( ELASTIX_BUILD_EXAMPLES "Enable building examples." ON ) diff --git a/SuperBuild/ExternalBoost.cmake b/SuperBuild/ExternalBoost.cmake index 4f27a44f..1047e94c 100644 --- a/SuperBuild/ExternalBoost.cmake +++ b/SuperBuild/ExternalBoost.cmake @@ -27,11 +27,10 @@ ExternalProject_Add( BOOST URL_MD5 ${BOOST_MD5} UPDATE_COMMAND "" CONFIGURE_COMMAND ${BOOST_CONFIGURE_COMMAND} - --prefix=${BOOST_BUILD_DIR}/lib BUILD_COMMAND "" INSTALL_COMMAND "" ) -set( BOOST_ROOT ${BOOST_BUILD_DIR} ) +set( BOOST_ROOT "${CMAKE_INSTALL_PREFIX}/${proj}-prefix/src/BOOST" ) list( APPEND ELASTIX_DEPENDENCIES ${proj} ) diff --git a/Testing/CMakeLists.txt b/Testing/CMakeLists.txt index 925c9375..3aea1e20 100644 --- a/Testing/CMakeLists.txt +++ b/Testing/CMakeLists.txt @@ -48,10 +48,6 @@ if( NOT EXISTS ${CMAKE_SOURCE_DIR}/Testing/GoogleTest/.git ) ) endif() -if( ${CMAKE_CXX_COMPILER_ID} STREQUAL "MSVC" ) - include( elxWinConfigGoogleTest.cmake ) -endif() - add_subdirectory( GoogleTest ) include_directories( diff --git a/Testing/Unit/CMakeLists.txt b/Testing/Unit/CMakeLists.txt index 1ed2189d..720b58c5 100644 --- a/Testing/Unit/CMakeLists.txt +++ b/Testing/Unit/CMakeLists.txt @@ -3,11 +3,26 @@ # Any GoogleTests in these files are automatically added to CTest and # the elastix dashboard. -set( ElastixUnitTestFilenames - elxExampleUnitTest.cxx - elxBluePrintTest.cxx - itkRegistration.cxx -) +# Not compiling elastix tests while we debug windows build. See minimum viable examples below +# set( ElastixUnitTestFilenames +# elxExampleUnitTest.cxx +# elxBlueprintTest.cxx +# ) + +# Pass +add_executable( HelloWorld HelloWorld.cxx) + +# Pass +add_executable( HelloWorldBlueprint HelloWorldBlueprint.cxx ) +target_link_libraries( HelloWorldBlueprint ${ELASTIX_LIBRARIES} ${ITK_LIBRARIES} ) + +# Fail. It seems GoogleTest is compiled statically (like it should), while the executable is compiled dynamically (it should also be static) Why? BUILD_SHARED_LIBS is set to OFF! + +# And why is the static linker option not recongnized (VS2013)? +set(CMAKE_EXE_LINKER_FLAGS /MTd ) + +add_executable( HelloWorldGoogleTest HelloWorldGoogleTest.cxx ) +target_link_libraries( HelloWorldGoogleTest ${TEST_LIBRARIES} ) # --------------------------------------------------------------------- # Set data directories diff --git a/Testing/Unit/HelloWorld.cxx b/Testing/Unit/HelloWorld.cxx new file mode 100644 index 00000000..9d908982 --- /dev/null +++ b/Testing/Unit/HelloWorld.cxx @@ -0,0 +1,7 @@ +#include <iostream> + +int main() +{ + std::cout << "Hello World!" << std::endl; + return 0; +} \ No newline at end of file diff --git a/Testing/Unit/HelloWorldBlueprint.cxx b/Testing/Unit/HelloWorldBlueprint.cxx new file mode 100644 index 00000000..1644eb24 --- /dev/null +++ b/Testing/Unit/HelloWorldBlueprint.cxx @@ -0,0 +1,12 @@ +#include <iostream> +#include "elxBlueprint.h" + +int main() +{ + elx::Blueprint::Pointer blueprint = elx::Blueprint::New(); + elx::Blueprint::ComponentIndexType index0 = blueprint->AddComponent(); + + std::cout << "Hello World!" << std::endl; + std::cout << "Loaded blueprint!" << std::endl; + return 0; +} diff --git a/Testing/Unit/HelloWorldGoogleTest.cxx b/Testing/Unit/HelloWorldGoogleTest.cxx new file mode 100644 index 00000000..7dcd6d3f --- /dev/null +++ b/Testing/Unit/HelloWorldGoogleTest.cxx @@ -0,0 +1,7 @@ +#include <iostream> +#include "gtest/gtest.h" + +TEST( HelloWorld, GoogleTest ) +{ + int a = 1; +} diff --git a/Testing/Unit/itkRegistration.cxx b/Testing/Unit/itkRegistration.cxx deleted file mode 100644 index 3988f308..00000000 --- a/Testing/Unit/itkRegistration.cxx +++ /dev/null @@ -1,86 +0,0 @@ -#include "itkImage.h" -#include "itkImageFileReader.h" -#include "elxDataManager.h" -#include "gtest/gtest.h" - -#include "itkImageRegistrationMethod.h" -#include "itkTranslationTransform.h" -#include "itkMeanSquaresImageToImageMetric.h" -#include "itkLinearInterpolateImageFunction.h" -#include "itkRegularStepGradientDescentOptimizer.h" -#include "itkImage.h" -#include "itkImageFileReader.h" -#include "itkResampleImageFilter.h" -#include "itkCastImageFilter.h" - -class itkRegistration : public ::testing::Test { -public: - virtual void SetUp() - { - // TODO: Loading images here result in segfault - } - - typedef itk::Image< unsigned short, 2 > ImageType; - - typedef itk::ImageFileReader< ImageType > FixedImageReaderType; - typedef itk::ImageFileReader< ImageType > MovingImageReaderType; - - FixedImageReaderType::Pointer fixedImageReader; - MovingImageReaderType::Pointer movingImageReader; - -}; - -TEST_F( itkRegistration, ImageRegistration3 ) -{ - - typedef itk::TranslationTransform< double, ImageType::ImageDimension > TransformType; - typedef itk::RegularStepGradientDescentOptimizer OptimizerType; - typedef itk::LinearInterpolateImageFunction< ImageType, double > InterpolatorType; - typedef itk::ImageRegistrationMethod< ImageType, ImageType > RegistrationType; - - - typedef itk::MeanSquaresImageToImageMetric< ImageType, ImageType > MetricType; - - TransformType::Pointer transform = TransformType::New(); - OptimizerType::Pointer optimizer = OptimizerType::New(); - InterpolatorType::Pointer interpolator = InterpolatorType::New(); - RegistrationType::Pointer registration = RegistrationType::New(); - - MetricType::Pointer metric = MetricType::New(); - registration->SetMetric( metric ); - registration->SetOptimizer( optimizer ); - registration->SetTransform( transform ); - registration->SetInterpolator( interpolator ); - - FixedImageReaderType::Pointer fixedImageReader = FixedImageReaderType::New(); - MovingImageReaderType::Pointer movingImageReader = MovingImageReaderType::New(); - - DataManager::Pointer dataManager = DataManager::New(); - - fixedImageReader->SetFileName( dataManager->GetInputFullPath( "BrainProtonDensitySlice.png" ) ); - movingImageReader->SetFileName( dataManager->GetInputFullPath( "BrainProtonDensitySliceR10X13Y17.png" ) ); - - registration->SetFixedImage( fixedImageReader->GetOutput() ); - registration->SetMovingImage( movingImageReader->GetOutput() ); - - fixedImageReader->Update(); // This is needed to make the BufferedRegion valid. - registration->SetFixedImageRegion( fixedImageReader->GetOutput()->GetBufferedRegion() ); - - typedef RegistrationType::ParametersType ParametersType; - ParametersType initialParameters( transform->GetNumberOfParameters() ); - - initialParameters[0] = 0.0; // Initial offset in mm along X - initialParameters[1] = 0.0; // Initial offset in mm along Y - - registration->SetInitialTransformParameters( initialParameters ); - - optimizer->SetMaximumStepLength( 4.00 ); - optimizer->SetMinimumStepLength( 0.01 ); - optimizer->SetNumberOfIterations( 200 ); - - optimizer->MaximizeOff(); - - EXPECT_NO_THROW( registration->Update() ); - RecordProperty( "MetricValue", optimizer->GetValue() ); -} - -- GitLab