stuart23 / cmake-OpenFOAM

Other
20 stars 1 forks source link

Could you add a findOpenFOAM script for existing OpenFOAM? #15

Open ghost opened 6 years ago

ghost commented 6 years ago

I think it would be helpful.

ghost commented 6 years ago

I came up with this idea because I can use the following script to build a solver using openfoam 6 docker image :


cmake_minimum_required (VERSION 3.8)

# cxx11
set( CMAKE_CXX_STANDARD 11 )
set(LIBS OpenFOAM dl m)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Xlinker --no-as-needed -Xlinker --add-needed")
link_directories($ENV{FOAM_LIBBIN})
add_definitions(-Dlinux64 -DWM_ARCH_OPTION=64 -DWM_DP -DWM_LABEL_SIZE=32 -DNoRepository -m64 -fPIC )

# Add source to this project's executable.
add_executable (laplacianFoam "laplacianFoam.C")

include_directories( $ENV{FOAM_SRC}/finiteVolume/lnInclude )
include_directories( $ENV{FOAM_SRC}/meshTools/lnInclude )
include_directories( $ENV{FOAM_SRC}/OpenFOAM/lnInclude )
include_directories( $ENV{FOAM_SRC}/OSspecific/POSIX/lnInclude )

set(LIBS finiteVolume fvOptions meshTools ${LIBS})

target_link_libraries(laplacianFoam ${LIBS})

So I think it is possible to write a separate findOpenFOAM script to get something like libOpenFOAM libMeshTool. Then users could write their own cmake script with easiness comparable to wmake's files and options file.

ghost commented 6 years ago

More example:

cmake_minimum_required (VERSION 3.2 FATAL_ERROR)

# project setting
project(laplacianFoam VERSION 6 LANGUAGES CXX)
set( SOURCES laplacianFoam.C)
add_executable( ${PROJECT_NAME} ${SOURCES} )

# common settings
## set PIC for executables, randonization make it hard to attack.
set_property(TARGET ${PROJECT_NAME} PROPERTY POSITION_INDEPENDENT_CODE ON) 
## recommended by CMake, see: https://rix0r.nl/blog/2015/08/13/cmake-guide/
include(GNUInstallDirs)

# create OpenFOAM target
add_library(OpenFOAM SHARED IMPORTED)
# cxx 11
target_compile_features(OpenFOAM INTERFACE cxx_std_11)
# include dirs
target_include_directories(OpenFOAM INTERFACE $ENV{FOAM_SRC}/OpenFOAM/lnInclude)
target_include_directories(OpenFOAM INTERFACE $ENV{FOAM_SRC}/OSspecific/POSIX/lnInclude)
# locate the libOpenFOAM.so file
unset(of_lib_path CACHE)
find_library(of_lib_path OpenFOAM PATHS $ENV{FOAM_LIBBIN})
set_property(TARGET OpenFOAM PROPERTY IMPORTED_LOCATION ${of_lib_path})
# definition
target_compile_definitions(OpenFOAM INTERFACE -Dlinux64 -DWM_ARCH_OPTION=64 -DWM_DP -DWM_LABEL_SIZE=32 -DNoRepository )
# depedent libs
target_link_libraries(OpenFOAM INTERFACE -ldl -lm)

# create finiteVolume target
add_library(finiteVolume SHARED IMPORTED)
target_include_directories(finiteVolume INTERFACE $ENV{FOAM_SRC}/finiteVolume/lnInclude)
# locate the libOpenFOAM.so file
unset(of_lib_path CACHE)
find_library(of_lib_path finiteVolume PATHS $ENV{FOAM_LIBBIN})
set_property(TARGET finiteVolume PROPERTY IMPORTED_LOCATION ${of_lib_path})
add_dependencies(finiteVolume OpenFOAM)

# create finiteVolume target
add_library(meshTools SHARED IMPORTED)
target_include_directories(meshTools INTERFACE $ENV{FOAM_SRC}/meshTools/lnInclude)
# locate the libOpenFOAM.so file
unset(of_lib_path CACHE)
find_library(of_lib_path meshTools PATHS $ENV{FOAM_LIBBIN})
set_property(TARGET meshTools PROPERTY IMPORTED_LOCATION ${of_lib_path})
add_dependencies(meshTools OpenFOAM)

# finalize setting
# warnings
target_compile_options(${PROJECT_NAME} PUBLIC -Wall -Wextra -Wold-style-cast -Wnon-virtual-dtor -Wno-unused-parameter -Wno-invalid-offsetof -Wno-attributes)
# link options
## `--add-needed` is replaced by `--copy-dt-needed-entries`
target_link_libraries(${PROJECT_NAME} PUBLIC -Wl,--copy-dt-needed-entries) 
## do not ignore supplied libraries even if they are unused. It seems useless now.
target_link_libraries(${PROJECT_NAME} PUBLIC -Wl,--no-as-needed )
# library dependencies, set final inc_dir,lib_dir automatically by deduction.  
target_link_libraries(${PROJECT_NAME} PUBLIC OpenFOAM meshTools finiteVolume)

# debug setting
set(CMAKE_VERBOSE_MAKEFILE ON)

# note
## `-m64` is unnecessary 

Comparing the command line of wmake and cmake:

# wmake

g++ -std=c++11 -m64 -Dlinux64 -DWM_ARCH_OPTION=64 -DWM_DP -DWM_LABEL_SIZE=32 -Wall -Wextra -Wold-style-cast -Wnon-virtual-dtor -Wno-unused-parameter -Wno-invalid-offsetof -Wno-attributes -O3  -DNoRepository -ftemplate-depth-100 -I/opt/openfoam6/src/finiteVolume/lnInclude -I/opt/openfoam6/src/meshTools/lnInclude -IlnInclude -I. -I/opt/openfoam6/src/OpenFOAM/lnInclude -I/opt/openfoam6/src/OSspecific/POSIX/lnInclude   -fPIC -c laplacianFoam.C -o Make/linux64GccDPInt32Opt/laplacianFoam.o

g++ -std=c++11 -m64 -Dlinux64 -DWM_ARCH_OPTION=64 -DWM_DP -DWM_LABEL_SIZE=32 -Wall -Wextra -Wold-style-cast -Wnon-virtual-dtor -Wno-unused-parameter -Wno-invalid-offsetof -Wno-attributes -O3  -DNoRepository -ftemplate-depth-100 -I/opt/openfoam6/src/finiteVolume/lnInclude -I/opt/openfoam6/src/meshTools/lnInclude -IlnInclude -I. -I/opt/openfoam6/src/OpenFOAM/lnInclude -I/opt/openfoam6/src/OSspecific/POSIX/lnInclude   -fPIC -Xlinker --add-needed -Xlinker --no-as-needed Make/linux64GccDPInt32Opt/laplacianFoam.o -L/opt/openfoam6/platforms/linux64GccDPInt32Opt/lib \
    -lfiniteVolume -lfvOptions -lmeshTools -lOpenFOAM -ldl  \
     -lm -o /home/openfoam/platforms/linux64GccDPInt32Opt/bin/laplacianFoam

#cmake

/usr/bin/c++  -DNoRepository -DWM_ARCH_OPTION=64 -DWM_DP -DWM_LABEL_SIZE=32 -Dlinux64 -isystem /opt/openfoam6/src/OpenFOAM/lnInclude -isystem /opt/openfoam6/src/OSspecific/POSIX/lnInclude -isystem /opt/openfoam6/src/meshTools/lnInclude -isystem /opt/openfoam6/src/finiteVolume/lnInclude  -g -fPIE   -Wall -Wextra -Wold-style-cast -Wnon-virtual-dtor -Wno-unused-parameter -Wno-invalid-offsetof -Wno-attributes -std=gnu++11 -o CMakeFiles/laplacianFoam.dir/laplacianFoam.C.o -c /var/tmp/src/5c7c854c-52b0-3333-9ad3-b92a426ff573/Linux-Debug/laplacianFoam.C

/usr/bin/c++  -g  -rdynamic CMakeFiles/laplacianFoam.dir/laplacianFoam.C.o  -o laplacianFoam -Wl,-rpath,/opt/openfoam6/platforms/linux64GccDPInt32Opt/lib -Wl,--copy-dt-needed-entries -Wl,--no-as-needed /opt/openfoam6/platforms/linux64GccDPInt32Opt/lib/libOpenFOAM.so /opt/openfoam6/platforms/linux64GccDPInt32Opt/lib/libmeshTools.so /opt/openfoam6/platforms/linux64GccDPInt32Opt/lib/libfiniteVolume.so -ldl -lm