https://public.kitware.com/Wiki/api.php?action=feedcontributions&user=Orbeckst&feedformat=atomKitwarePublic - User contributions [en]2024-03-28T21:18:03ZUser contributionsMediaWiki 1.38.6https://public.kitware.com/Wiki/index.php?title=CMakeForFortranExample&diff=14096CMakeForFortranExample2008-11-21T16:42:38Z<p>Orbeckst: Undo revision 14095 by Orbeckst (Talk) oops again - FORCE is needed to make the variables show up in the interactive configuration</p>
<hr />
<div>[[CMake]] is perfectly capable of handling Fortran code; however, examples are less abundant than for C or C++. In the example below we write a very small [[CMakeLists.txt]] to wrap a simple Fortran project. <br />
<br />
The motivation for this was to easily compile Fortran code that came with the [http://www.igs.cnrs-mrs.fr/elnemo/NORMA/ NORMA] package. The distribution NORMA-1.0.tgz was downloaded and unpacked. [[#Example CMakeLists.txt|following <tt>CMakeLists.txt</tt>]] was placed into the <tt>./software</tt> directory where the NORMA sources are stored.<br />
<br />
To build one would typically create a build directory at the same level as <tt>software</tt>:<br />
mkdir build && cd build<br />
ccmake ../software<br />
make<br />
make install<br />
and then configure, build and install. <br />
<br />
The project is very simple:<br />
* There are only a few fortran source code files in the software directory.<br />
* There are no library dependencies.<br />
* Executables are typically installed in a bin directory in the package itself (i.e. <tt>NORMA/bin</tt>) although the user is free to to change the installation prefix.<br />
* Two shell scripts are also installed alongside.<br />
<br />
The <tt>CMakeLists.txt</tt> is commented and [[#Comments on CMakeLists.txt|additional comments]] can be found after it.<br />
<br />
== Example CMakeLists.txt ==<br />
<pre># CMake project file for NORMA<br />
<br />
cmake_minimum_required (VERSION 2.6)<br />
project (NORMA)<br />
enable_language (Fortran)<br />
<br />
# make sure that the default is a RELEASE<br />
if (NOT CMAKE_BUILD_TYPE)<br />
set (CMAKE_BUILD_TYPE RELEASE CACHE STRING<br />
"Choose the type of build, options are: None Debug Release."<br />
FORCE)<br />
endif (NOT CMAKE_BUILD_TYPE)<br />
<br />
# default installation<br />
get_filename_component (default_prefix ".." ABSOLUTE)<br />
set (CMAKE_INSTALL_PREFIX ${default_prefix} CACHE STRING<br />
"Choose the installation directory; by default it installs in the NORMA directory."<br />
FORCE)<br />
<br />
# FFLAGS depend on the compiler<br />
get_filename_component (Fortran_COMPILER_NAME ${CMAKE_Fortran_COMPILER} NAME)<br />
<br />
if (Fortran_COMPILER_NAME STREQUAL "gfortran")<br />
# gfortran<br />
set (CMAKE_Fortran_FLAGS_RELEASE "-funroll-all-loops -fno-f2c -O3")<br />
set (CMAKE_Fortran_FLAGS_DEBUG "-fno-f2c -O0 -g")<br />
elseif (Fortran_COMPILER_NAME STREQUAL "ifort")<br />
# ifort (untested)<br />
set (CMAKE_Fortran_FLAGS_RELEASE "-f77rtl -O3")<br />
set (CMAKE_Fortran_FLAGS_DEBUG "-f77rtl -O0 -g")<br />
elseif (Fortran_COMPILER_NAME STREQUAL "g77")<br />
# g77<br />
set (CMAKE_Fortran_FLAGS_RELEASE "-funroll-all-loops -fno-f2c -O3 -m32")<br />
set (CMAKE_Fortran_FLAGS_DEBUG "-fno-f2c -O0 -g -m32")<br />
else (Fortran_COMPILER_NAME STREQUAL "gfortran")<br />
message ("CMAKE_Fortran_COMPILER full path: " ${CMAKE_Fortran_COMPILER})<br />
message ("Fortran compiler: " ${Fortran_COMPILER_NAME})<br />
message ("No optimized Fortran compiler flags are known, we just try -O2...")<br />
set (CMAKE_Fortran_FLAGS_RELEASE "-O2")<br />
set (CMAKE_Fortran_FLAGS_DEBUG "-O0 -g")<br />
endif (Fortran_COMPILER_NAME STREQUAL "gfortran")<br />
<br />
<br />
# build executables<br />
set (NMPROGRAMS "diagstd" "diagrtb" "proj_modes_bin" "pdbmat")<br />
set (EXECUTABLES "NORMA.exe" ${NMPROGRAMS})<br />
set (SCRIPTS "gen_pert.sh" "pert_multi_mode.sh")<br />
<br />
add_executable ("NORMA.exe" "NORMA.f")<br />
foreach (p ${NMPROGRAMS})<br />
add_executable (${p} "${p}.f")<br />
endforeach (p)<br />
<br />
# install executables and scripts<br />
install (TARGETS ${EXECUTABLES} <br />
RUNTIME DESTINATION "bin")<br />
install (PROGRAMS ${SCRIPTS}<br />
DESTINATION "bin") <br />
</pre><br />
<br />
== Comments on CMakeLists.txt ==<br />
* Use cmake v2.6 or better for [[CMake Fortran Issues|optimum Fortran support]].<br />
* Make sure that we build a RELEASE by default (and let the user know in the GUI); uses example code from the docs.<br />
* Set the default prefix to the non-standard installation scheme in which one installs all component into the package directory. This directory is assumed to be one above relative the build directory. The full canonical path is obtained with the [http://www.cmake.org/cmake/help/cmake2.6docs.html#command:get_filename_component get_filename_component] function.<br />
* As it is typical for scientific Fortran code, the compiler optimizations are fairly specific. Here we are trying to detect a few common ones (gfortran, Intel ifort, g77) and set the compile options accordingly. If all else fails we use -O2 and hope for the best (but let the user know).<br />
* The <tt>NORMA.exe</tt> binary is built from the source <tt>NORMA.f</tt> so we have to say this explicitly with the <pre>add_executable ("NORMA.exe" "NORMA.f")</pre>line but the other binaries are simply built from corresponding .f files and we write this as a simple foreach-loop.<br />
* Executables are installed in the <tt>bin</tt> dir (which is relative to the prefix).<br />
* Finally, scripts are copied to the <tt>bin</tt> directory, too.</div>Orbecksthttps://public.kitware.com/Wiki/index.php?title=CMakeForFortranExample&diff=14095CMakeForFortranExample2008-11-21T16:21:12Z<p>Orbeckst: /* Example CMakeLists.txt */ don't FORCE settings (otherwise user cannot change them)</p>
<hr />
<div>[[CMake]] is perfectly capable of handling Fortran code; however, examples are less abundant than for C or C++. In the example below we write a very small [[CMakeLists.txt]] to wrap a simple Fortran project. <br />
<br />
The motivation for this was to easily compile Fortran code that came with the [http://www.igs.cnrs-mrs.fr/elnemo/NORMA/ NORMA] package. The distribution NORMA-1.0.tgz was downloaded and unpacked. [[#Example CMakeLists.txt|following <tt>CMakeLists.txt</tt>]] was placed into the <tt>./software</tt> directory where the NORMA sources are stored.<br />
<br />
To build one would typically create a build directory at the same level as <tt>software</tt>:<br />
mkdir build && cd build<br />
ccmake ../software<br />
make<br />
make install<br />
and then configure, build and install. <br />
<br />
The project is very simple:<br />
* There are only a few fortran source code files in the software directory.<br />
* There are no library dependencies.<br />
* Executables are typically installed in a bin directory in the package itself (i.e. <tt>NORMA/bin</tt>) although the user is free to to change the installation prefix.<br />
* Two shell scripts are also installed alongside.<br />
<br />
The <tt>CMakeLists.txt</tt> is commented and [[#Comments on CMakeLists.txt|additional comments]] can be found after it.<br />
<br />
== Example CMakeLists.txt ==<br />
<pre># CMake project file for NORMA<br />
<br />
cmake_minimum_required (VERSION 2.6)<br />
project (NORMA)<br />
enable_language (Fortran)<br />
<br />
# make sure that the default is a RELEASE<br />
if (NOT CMAKE_BUILD_TYPE)<br />
set (CMAKE_BUILD_TYPE RELEASE CACHE STRING<br />
"Choose the type of build, options are: None Debug Release.")<br />
endif (NOT CMAKE_BUILD_TYPE)<br />
<br />
# default installation<br />
get_filename_component (default_prefix ".." ABSOLUTE)<br />
set (CMAKE_INSTALL_PREFIX ${default_prefix} CACHE STRING<br />
"Choose the installation directory; by default it installs in the NORMA directory.")<br />
<br />
# FFLAGS depend on the compiler<br />
get_filename_component (Fortran_COMPILER_NAME ${CMAKE_Fortran_COMPILER} NAME)<br />
<br />
if (Fortran_COMPILER_NAME STREQUAL "gfortran")<br />
# gfortran<br />
set (CMAKE_Fortran_FLAGS_RELEASE "-funroll-all-loops -fno-f2c -O3")<br />
set (CMAKE_Fortran_FLAGS_DEBUG "-fno-f2c -O0 -g")<br />
elseif (Fortran_COMPILER_NAME STREQUAL "ifort")<br />
# ifort (untested)<br />
set (CMAKE_Fortran_FLAGS_RELEASE "-f77rtl -O3")<br />
set (CMAKE_Fortran_FLAGS_DEBUG "-f77rtl -O0 -g")<br />
elseif (Fortran_COMPILER_NAME STREQUAL "g77")<br />
# g77<br />
set (CMAKE_Fortran_FLAGS_RELEASE "-funroll-all-loops -fno-f2c -O3 -m32")<br />
set (CMAKE_Fortran_FLAGS_DEBUG "-fno-f2c -O0 -g -m32")<br />
else (Fortran_COMPILER_NAME STREQUAL "gfortran")<br />
message ("CMAKE_Fortran_COMPILER full path: " ${CMAKE_Fortran_COMPILER})<br />
message ("Fortran compiler: " ${Fortran_COMPILER_NAME})<br />
message ("No optimized Fortran compiler flags are known, we just try -O2...")<br />
set (CMAKE_Fortran_FLAGS_RELEASE "-O2")<br />
set (CMAKE_Fortran_FLAGS_DEBUG "-O0 -g")<br />
endif (Fortran_COMPILER_NAME STREQUAL "gfortran")<br />
<br />
<br />
# build executables<br />
set (NMPROGRAMS "diagstd" "diagrtb" "proj_modes_bin" "pdbmat")<br />
set (EXECUTABLES "NORMA.exe" ${NMPROGRAMS})<br />
set (SCRIPTS "gen_pert.sh" "pert_multi_mode.sh")<br />
<br />
add_executable ("NORMA.exe" "NORMA.f")<br />
foreach (p ${NMPROGRAMS})<br />
add_executable (${p} "${p}.f")<br />
endforeach (p)<br />
<br />
# install executables and scripts<br />
install (TARGETS ${EXECUTABLES} <br />
RUNTIME DESTINATION "bin")<br />
install (PROGRAMS ${SCRIPTS}<br />
DESTINATION "bin") <br />
</pre><br />
<br />
== Comments on CMakeLists.txt ==<br />
* Use cmake v2.6 or better for [[CMake Fortran Issues|optimum Fortran support]].<br />
* Make sure that we build a RELEASE by default (and let the user know in the GUI); uses example code from the docs.<br />
* Set the default prefix to the non-standard installation scheme in which one installs all component into the package directory. This directory is assumed to be one above relative the build directory. The full canonical path is obtained with the [http://www.cmake.org/cmake/help/cmake2.6docs.html#command:get_filename_component get_filename_component] function.<br />
* As it is typical for scientific Fortran code, the compiler optimizations are fairly specific. Here we are trying to detect a few common ones (gfortran, Intel ifort, g77) and set the compile options accordingly. If all else fails we use -O2 and hope for the best (but let the user know).<br />
* The <tt>NORMA.exe</tt> binary is built from the source <tt>NORMA.f</tt> so we have to say this explicitly with the <pre>add_executable ("NORMA.exe" "NORMA.f")</pre>line but the other binaries are simply built from corresponding .f files and we write this as a simple foreach-loop.<br />
* Executables are installed in the <tt>bin</tt> dir (which is relative to the prefix).<br />
* Finally, scripts are copied to the <tt>bin</tt> directory, too.</div>Orbecksthttps://public.kitware.com/Wiki/index.php?title=CMakeForFortranExample&diff=14094CMakeForFortranExample2008-11-21T15:11:47Z<p>Orbeckst: Undo revision 14093 by Orbeckst (Talk) -- oops, ccmake was right</p>
<hr />
<div>[[CMake]] is perfectly capable of handling Fortran code; however, examples are less abundant than for C or C++. In the example below we write a very small [[CMakeLists.txt]] to wrap a simple Fortran project. <br />
<br />
The motivation for this was to easily compile Fortran code that came with the [http://www.igs.cnrs-mrs.fr/elnemo/NORMA/ NORMA] package. The distribution NORMA-1.0.tgz was downloaded and unpacked. [[#Example CMakeLists.txt|following <tt>CMakeLists.txt</tt>]] was placed into the <tt>./software</tt> directory where the NORMA sources are stored.<br />
<br />
To build one would typically create a build directory at the same level as <tt>software</tt>:<br />
mkdir build && cd build<br />
ccmake ../software<br />
make<br />
make install<br />
and then configure, build and install. <br />
<br />
The project is very simple:<br />
* There are only a few fortran source code files in the software directory.<br />
* There are no library dependencies.<br />
* Executables are typically installed in a bin directory in the package itself (i.e. <tt>NORMA/bin</tt>) although the user is free to to change the installation prefix.<br />
* Two shell scripts are also installed alongside.<br />
<br />
The <tt>CMakeLists.txt</tt> is commented and [[#Comments on CMakeLists.txt|additional comments]] can be found after it.<br />
<br />
== Example CMakeLists.txt ==<br />
<pre># CMake project file for NORMA<br />
<br />
cmake_minimum_required (VERSION 2.6)<br />
project (NORMA)<br />
enable_language (Fortran)<br />
<br />
# make sure that the default is a RELEASE<br />
if (NOT CMAKE_BUILD_TYPE)<br />
set (CMAKE_BUILD_TYPE RELEASE CACHE STRING<br />
"Choose the type of build, options are: None Debug Release."<br />
FORCE)<br />
endif (NOT CMAKE_BUILD_TYPE)<br />
<br />
# default installation<br />
get_filename_component (default_prefix ".." ABSOLUTE)<br />
set (CMAKE_INSTALL_PREFIX ${default_prefix} CACHE STRING<br />
"Choose the installation directory; by default it installs in the NORMA directory."<br />
FORCE)<br />
<br />
# FFLAGS depend on the compiler<br />
get_filename_component (Fortran_COMPILER_NAME ${CMAKE_Fortran_COMPILER} NAME)<br />
<br />
if (Fortran_COMPILER_NAME STREQUAL "gfortran")<br />
# gfortran<br />
set (CMAKE_Fortran_FLAGS_RELEASE "-funroll-all-loops -fno-f2c -O3")<br />
set (CMAKE_Fortran_FLAGS_DEBUG "-fno-f2c -O0 -g")<br />
elseif (Fortran_COMPILER_NAME STREQUAL "ifort")<br />
# ifort (untested)<br />
set (CMAKE_Fortran_FLAGS_RELEASE "-f77rtl -O3")<br />
set (CMAKE_Fortran_FLAGS_DEBUG "-f77rtl -O0 -g")<br />
elseif (Fortran_COMPILER_NAME STREQUAL "g77")<br />
# g77<br />
set (CMAKE_Fortran_FLAGS_RELEASE "-funroll-all-loops -fno-f2c -O3 -m32")<br />
set (CMAKE_Fortran_FLAGS_DEBUG "-fno-f2c -O0 -g -m32")<br />
else (Fortran_COMPILER_NAME STREQUAL "gfortran")<br />
message ("CMAKE_Fortran_COMPILER full path: " ${CMAKE_Fortran_COMPILER})<br />
message ("Fortran compiler: " ${Fortran_COMPILER_NAME})<br />
message ("No optimized Fortran compiler flags are known, we just try -O2...")<br />
set (CMAKE_Fortran_FLAGS_RELEASE "-O2")<br />
set (CMAKE_Fortran_FLAGS_DEBUG "-O0 -g")<br />
endif (Fortran_COMPILER_NAME STREQUAL "gfortran")<br />
<br />
<br />
# build executables<br />
set (NMPROGRAMS "diagstd" "diagrtb" "proj_modes_bin" "pdbmat")<br />
set (EXECUTABLES "NORMA.exe" ${NMPROGRAMS})<br />
set (SCRIPTS "gen_pert.sh" "pert_multi_mode.sh")<br />
<br />
add_executable ("NORMA.exe" "NORMA.f")<br />
foreach (p ${NMPROGRAMS})<br />
add_executable (${p} "${p}.f")<br />
endforeach (p)<br />
<br />
# install executables and scripts<br />
install (TARGETS ${EXECUTABLES} <br />
RUNTIME DESTINATION "bin")<br />
install (PROGRAMS ${SCRIPTS}<br />
DESTINATION "bin") <br />
</pre><br />
<br />
== Comments on CMakeLists.txt ==<br />
* Use cmake v2.6 or better for [[CMake Fortran Issues|optimum Fortran support]].<br />
* Make sure that we build a RELEASE by default (and let the user know in the GUI); uses example code from the docs.<br />
* Set the default prefix to the non-standard installation scheme in which one installs all component into the package directory. This directory is assumed to be one above relative the build directory. The full canonical path is obtained with the [http://www.cmake.org/cmake/help/cmake2.6docs.html#command:get_filename_component get_filename_component] function.<br />
* As it is typical for scientific Fortran code, the compiler optimizations are fairly specific. Here we are trying to detect a few common ones (gfortran, Intel ifort, g77) and set the compile options accordingly. If all else fails we use -O2 and hope for the best (but let the user know).<br />
* The <tt>NORMA.exe</tt> binary is built from the source <tt>NORMA.f</tt> so we have to say this explicitly with the <pre>add_executable ("NORMA.exe" "NORMA.f")</pre>line but the other binaries are simply built from corresponding .f files and we write this as a simple foreach-loop.<br />
* Executables are installed in the <tt>bin</tt> dir (which is relative to the prefix).<br />
* Finally, scripts are copied to the <tt>bin</tt> directory, too.</div>Orbecksthttps://public.kitware.com/Wiki/index.php?title=CMakeForFortranExample&diff=14093CMakeForFortranExample2008-11-21T14:46:20Z<p>Orbeckst: fixed: ccmake -> cmake</p>
<hr />
<div>[[CMake]] is perfectly capable of handling Fortran code; however, examples are less abundant than for C or C++. In the example below we write a very small [[CMakeLists.txt]] to wrap a simple Fortran project. <br />
<br />
The motivation for this was to easily compile Fortran code that came with the [http://www.igs.cnrs-mrs.fr/elnemo/NORMA/ NORMA] package. The distribution NORMA-1.0.tgz was downloaded and unpacked. [[#Example CMakeLists.txt|following <tt>CMakeLists.txt</tt>]] was placed into the <tt>./software</tt> directory where the NORMA sources are stored.<br />
<br />
To build one would typically create a build directory at the same level as <tt>software</tt>:<br />
mkdir build && cd build<br />
cmake ../software<br />
make<br />
make install<br />
and then configure, build and install. <br />
<br />
The project is very simple:<br />
* There are only a few fortran source code files in the software directory.<br />
* There are no library dependencies.<br />
* Executables are typically installed in a bin directory in the package itself (i.e. <tt>NORMA/bin</tt>) although the user is free to to change the installation prefix.<br />
* Two shell scripts are also installed alongside.<br />
<br />
The <tt>CMakeLists.txt</tt> is commented and [[#Comments on CMakeLists.txt|additional comments]] can be found after it.<br />
<br />
== Example CMakeLists.txt ==<br />
<pre># CMake project file for NORMA<br />
<br />
cmake_minimum_required (VERSION 2.6)<br />
project (NORMA)<br />
enable_language (Fortran)<br />
<br />
# make sure that the default is a RELEASE<br />
if (NOT CMAKE_BUILD_TYPE)<br />
set (CMAKE_BUILD_TYPE RELEASE CACHE STRING<br />
"Choose the type of build, options are: None Debug Release."<br />
FORCE)<br />
endif (NOT CMAKE_BUILD_TYPE)<br />
<br />
# default installation<br />
get_filename_component (default_prefix ".." ABSOLUTE)<br />
set (CMAKE_INSTALL_PREFIX ${default_prefix} CACHE STRING<br />
"Choose the installation directory; by default it installs in the NORMA directory."<br />
FORCE)<br />
<br />
# FFLAGS depend on the compiler<br />
get_filename_component (Fortran_COMPILER_NAME ${CMAKE_Fortran_COMPILER} NAME)<br />
<br />
if (Fortran_COMPILER_NAME STREQUAL "gfortran")<br />
# gfortran<br />
set (CMAKE_Fortran_FLAGS_RELEASE "-funroll-all-loops -fno-f2c -O3")<br />
set (CMAKE_Fortran_FLAGS_DEBUG "-fno-f2c -O0 -g")<br />
elseif (Fortran_COMPILER_NAME STREQUAL "ifort")<br />
# ifort (untested)<br />
set (CMAKE_Fortran_FLAGS_RELEASE "-f77rtl -O3")<br />
set (CMAKE_Fortran_FLAGS_DEBUG "-f77rtl -O0 -g")<br />
elseif (Fortran_COMPILER_NAME STREQUAL "g77")<br />
# g77<br />
set (CMAKE_Fortran_FLAGS_RELEASE "-funroll-all-loops -fno-f2c -O3 -m32")<br />
set (CMAKE_Fortran_FLAGS_DEBUG "-fno-f2c -O0 -g -m32")<br />
else (Fortran_COMPILER_NAME STREQUAL "gfortran")<br />
message ("CMAKE_Fortran_COMPILER full path: " ${CMAKE_Fortran_COMPILER})<br />
message ("Fortran compiler: " ${Fortran_COMPILER_NAME})<br />
message ("No optimized Fortran compiler flags are known, we just try -O2...")<br />
set (CMAKE_Fortran_FLAGS_RELEASE "-O2")<br />
set (CMAKE_Fortran_FLAGS_DEBUG "-O0 -g")<br />
endif (Fortran_COMPILER_NAME STREQUAL "gfortran")<br />
<br />
<br />
# build executables<br />
set (NMPROGRAMS "diagstd" "diagrtb" "proj_modes_bin" "pdbmat")<br />
set (EXECUTABLES "NORMA.exe" ${NMPROGRAMS})<br />
set (SCRIPTS "gen_pert.sh" "pert_multi_mode.sh")<br />
<br />
add_executable ("NORMA.exe" "NORMA.f")<br />
foreach (p ${NMPROGRAMS})<br />
add_executable (${p} "${p}.f")<br />
endforeach (p)<br />
<br />
# install executables and scripts<br />
install (TARGETS ${EXECUTABLES} <br />
RUNTIME DESTINATION "bin")<br />
install (PROGRAMS ${SCRIPTS}<br />
DESTINATION "bin") <br />
</pre><br />
<br />
== Comments on CMakeLists.txt ==<br />
* Use cmake v2.6 or better for [[CMake Fortran Issues|optimum Fortran support]].<br />
* Make sure that we build a RELEASE by default (and let the user know in the GUI); uses example code from the docs.<br />
* Set the default prefix to the non-standard installation scheme in which one installs all component into the package directory. This directory is assumed to be one above relative the build directory. The full canonical path is obtained with the [http://www.cmake.org/cmake/help/cmake2.6docs.html#command:get_filename_component get_filename_component] function.<br />
* As it is typical for scientific Fortran code, the compiler optimizations are fairly specific. Here we are trying to detect a few common ones (gfortran, Intel ifort, g77) and set the compile options accordingly. If all else fails we use -O2 and hope for the best (but let the user know).<br />
* The <tt>NORMA.exe</tt> binary is built from the source <tt>NORMA.f</tt> so we have to say this explicitly with the <pre>add_executable ("NORMA.exe" "NORMA.f")</pre>line but the other binaries are simply built from corresponding .f files and we write this as a simple foreach-loop.<br />
* Executables are installed in the <tt>bin</tt> dir (which is relative to the prefix).<br />
* Finally, scripts are copied to the <tt>bin</tt> directory, too.</div>Orbecksthttps://public.kitware.com/Wiki/index.php?title=Talk:CMakeForFortranExample&diff=13889Talk:CMakeForFortranExample2008-10-21T13:41:47Z<p>Orbeckst: /* Disclaimer */</p>
<hr />
<div>== Disclaimer ==<br />
I am not in any way affiliated with the NORMA software I mention in the [[CMakeForFortranExample|article]]. I just happened to have a need for building it and thought to try [[CMake]] &mdash; which I already like very much. But this also means that I am ''an absolute beginner'' when it comes to CMake so I hope that more enlightened readers will correct my mistakes. I just hadn't seen examples dealing with Fortran on the Wiki or the official docs so I thought this could potentially be useful to others. Feel free to contact me through the wiki if there are questions. &mdash; [[User:Orbeckst|Oliver]] 13:41, 21 October 2008 (UTC)</div>Orbecksthttps://public.kitware.com/Wiki/index.php?title=Talk:CMakeForFortranExample&diff=13888Talk:CMakeForFortranExample2008-10-21T13:40:03Z<p>Orbeckst: orbeckst's disclaimer</p>
<hr />
<div>== Disclaimer ==<br />
I am not in any way affiliated with the NORMA software I mention in the [[CMakeForFortranExample|article]]. I just happened to have a need for building it and thought to try [[CMake]] &mdash; which I already like very much. But this also means that I am ''an absolute beginner'' when it comes to CMake so I hope that more enlightened readers will correct my mistakes. I just hadn't seen examples dealing with Fortran on the Wiki or the official docs so I thought this could potentially be useful to others. &mdash; [[User:Orbeckst|Orbeckst]] 13:40, 21 October 2008 (UTC)</div>Orbecksthttps://public.kitware.com/Wiki/index.php?title=CMakeForFortranExample&diff=13887CMakeForFortranExample2008-10-21T13:35:28Z<p>Orbeckst: simple (real life) example of how CMake-ify legacy Fortran code</p>
<hr />
<div>[[CMake]] is perfectly capable of handling Fortran code; however, examples are less abundant than for C or C++. In the example below we write a very small [[CMakeLists.txt]] to wrap a simple Fortran project. <br />
<br />
The motivation for this was to easily compile Fortran code that came with the [http://www.igs.cnrs-mrs.fr/elnemo/NORMA/ NORMA] package. The distribution NORMA-1.0.tgz was downloaded and unpacked. [[#Example CMakeLists.txt|following <tt>CMakeLists.txt</tt>]] was placed into the <tt>./software</tt> directory where the NORMA sources are stored.<br />
<br />
To build one would typically create a build directory at the same level as <tt>software</tt>:<br />
mkdir build && cd build<br />
ccmake ../software<br />
make<br />
make install<br />
and then configure, build and install. <br />
<br />
The project is very simple:<br />
* There are only a few fortran source code files in the software directory.<br />
* There are no library dependencies.<br />
* Executables are typically installed in a bin directory in the package itself (i.e. <tt>NORMA/bin</tt>) although the user is free to to change the installation prefix.<br />
* Two shell scripts are also installed alongside.<br />
<br />
The <tt>CMakeLists.txt</tt> is commented and [[#Comments on CMakeLists.txt|additional comments]] can be found after it.<br />
<br />
== Example CMakeLists.txt ==<br />
<pre># CMake project file for NORMA<br />
<br />
cmake_minimum_required (VERSION 2.6)<br />
project (NORMA)<br />
enable_language (Fortran)<br />
<br />
# make sure that the default is a RELEASE<br />
if (NOT CMAKE_BUILD_TYPE)<br />
set (CMAKE_BUILD_TYPE RELEASE CACHE STRING<br />
"Choose the type of build, options are: None Debug Release."<br />
FORCE)<br />
endif (NOT CMAKE_BUILD_TYPE)<br />
<br />
# default installation<br />
get_filename_component (default_prefix ".." ABSOLUTE)<br />
set (CMAKE_INSTALL_PREFIX ${default_prefix} CACHE STRING<br />
"Choose the installation directory; by default it installs in the NORMA directory."<br />
FORCE)<br />
<br />
# FFLAGS depend on the compiler<br />
get_filename_component (Fortran_COMPILER_NAME ${CMAKE_Fortran_COMPILER} NAME)<br />
<br />
if (Fortran_COMPILER_NAME STREQUAL "gfortran")<br />
# gfortran<br />
set (CMAKE_Fortran_FLAGS_RELEASE "-funroll-all-loops -fno-f2c -O3")<br />
set (CMAKE_Fortran_FLAGS_DEBUG "-fno-f2c -O0 -g")<br />
elseif (Fortran_COMPILER_NAME STREQUAL "ifort")<br />
# ifort (untested)<br />
set (CMAKE_Fortran_FLAGS_RELEASE "-f77rtl -O3")<br />
set (CMAKE_Fortran_FLAGS_DEBUG "-f77rtl -O0 -g")<br />
elseif (Fortran_COMPILER_NAME STREQUAL "g77")<br />
# g77<br />
set (CMAKE_Fortran_FLAGS_RELEASE "-funroll-all-loops -fno-f2c -O3 -m32")<br />
set (CMAKE_Fortran_FLAGS_DEBUG "-fno-f2c -O0 -g -m32")<br />
else (Fortran_COMPILER_NAME STREQUAL "gfortran")<br />
message ("CMAKE_Fortran_COMPILER full path: " ${CMAKE_Fortran_COMPILER})<br />
message ("Fortran compiler: " ${Fortran_COMPILER_NAME})<br />
message ("No optimized Fortran compiler flags are known, we just try -O2...")<br />
set (CMAKE_Fortran_FLAGS_RELEASE "-O2")<br />
set (CMAKE_Fortran_FLAGS_DEBUG "-O0 -g")<br />
endif (Fortran_COMPILER_NAME STREQUAL "gfortran")<br />
<br />
<br />
# build executables<br />
set (NMPROGRAMS "diagstd" "diagrtb" "proj_modes_bin" "pdbmat")<br />
set (EXECUTABLES "NORMA.exe" ${NMPROGRAMS})<br />
set (SCRIPTS "gen_pert.sh" "pert_multi_mode.sh")<br />
<br />
add_executable ("NORMA.exe" "NORMA.f")<br />
foreach (p ${NMPROGRAMS})<br />
add_executable (${p} "${p}.f")<br />
endforeach (p)<br />
<br />
# install executables and scripts<br />
install (TARGETS ${EXECUTABLES} <br />
RUNTIME DESTINATION "bin")<br />
install (PROGRAMS ${SCRIPTS}<br />
DESTINATION "bin") <br />
</pre><br />
<br />
== Comments on CMakeLists.txt ==<br />
* Use cmake v2.6 or better for [[CMake Fortran Issues|optimum Fortran support]].<br />
* Make sure that we build a RELEASE by default (and let the user know in the GUI); uses example code from the docs.<br />
* Set the default prefix to the non-standard installation scheme in which one installs all component into the package directory. This directory is assumed to be one above relative the build directory. The full canonical path is obtained with the [http://www.cmake.org/cmake/help/cmake2.6docs.html#command:get_filename_component get_filename_component] function.<br />
* As it is typical for scientific Fortran code, the compiler optimizations are fairly specific. Here we are trying to detect a few common ones (gfortran, Intel ifort, g77) and set the compile options accordingly. If all else fails we use -O2 and hope for the best (but let the user know).<br />
* The <tt>NORMA.exe</tt> binary is built from the source <tt>NORMA.f</tt> so we have to say this explicitly with the <pre>add_executable ("NORMA.exe" "NORMA.f")</pre>line but the other binaries are simply built from corresponding .f files and we write this as a simple foreach-loop.<br />
* Executables are installed in the <tt>bin</tt> dir (which is relative to the prefix).<br />
* Finally, scripts are copied to the <tt>bin</tt> directory, too.</div>Orbecksthttps://public.kitware.com/Wiki/index.php?title=CMake&diff=13886CMake2008-10-21T11:19:20Z<p>Orbeckst: /* Specific Topics */ link to a simple Fortran example</p>
<hr />
<div>http://www.cmake.org/cmake/img/CMake-logo-download.jpg<br />
<br />
<!-- documentation manual man information help tutorial --><br />
Welcome to CMake, the cross-platform, open-source make system. CMake is used to control the software compilation process using simple platform and compiler independent configuration files. CMake generates native makefiles and workspaces that can be used in the compiler environment of your choice. CMake is quite sophisticated: it is possible to support complex environments requiring system configuration, pre-processor generation, code generation, and template instantiation.<br />
<br />
You will find here not only documentation for CMake, but also for CPack and CTest.<br />
<br />
==CMake ==<br />
<br />
=== Primary Resources - Look here first! ===<br />
* Where can I [http://www.cmake.org/HTML/Download.html download CMake]?<br />
* [http://www.cmake.org/HTML/Documentation.html CMake Documentation]<br />
* [[CMake Useful Variables|Useful CMake Variables]]<br />
* [[CMake FAQ| FAQ (Frequently asked questions)]]<br />
* [http://www.cmake.org/mailman/listinfo/cmake CMake Mailing List]<br />
* [[CMake_2.6_Notes|CMake 2.6 Notes]]<br />
<br />
=== Development Topics===<br />
* [[CMake Cross Compiling| Cross compiling]]<br />
* [[CMake RPATH handling|RPATH handling]]<br />
* [[CMake Editors Support|Editors/IDEs with CMake syntax support]]<br />
* [[CMake Generator Specific Information|Docs for Specific Project Generators]] (Eclipse, KDevelop3, CodeBlocks, Makefile)<br />
* [[CMake User Contributed Macros| Contributed macros]]<br />
* [[CMake:Module Maintainers|Module Maintainers]]<br />
* [[CMake:VariablesListsStrings| On variables, lists, strings, maps, regexps, etc.]]<br />
* [[CMake Platform Dependent Issues|Platform Dependent Information]]<br />
* [[CMake Released Versions|Documentation for previous releases]]<br />
<br />
===Tutorials===<br />
<br />
====Basic Introductions====<br />
* [http://www.cmake.org/HTML/Examples.html A Simple CMake Example]<br />
* [http://www.linuxjournal.com/article/6700 Cross-Platform Software Development Using CMake]<br />
* [http://clubjuggler.livejournal.com/138364.html CMake: The Cross Platform Build System]<br />
* [http://www.elpauer.org/stuff/learning_cmake.pdf "Learning CMake"] - Slides of a CMake workshop (somewhat updated for CMake 2.6, include CPack, CTest and CDash information)<br />
* Syntax of the CMake language<br />
** [http://www.cmake.org/HTML/syntax.html A quick introduction to CMake syntax]<br />
** [[/Language Syntax]] (wiki page)<br />
<br />
====Specific Topics====<br />
* [[CMake:Eclipse_UNIX_Tutorial | How to use CMake with Eclipse CDT]] <br> Describes how to use CMake with Eclipse CDT.<br />
* [[CMake HowToDoPlatformChecks| How to write platform checks with CMake]]<br>Describes how to implement platform or configure checks with CMake.<br />
<br />
* [[CMake HowToFindInstalledSoftware| How to find installed software with CMake]]<br>Describes how to search for and then use software packages with CMake.<br />
<br />
* [[CMake:HowToUseExistingOSXFrameworks | How to find and use existing frameworks on OS X]]<br> A quick example to help OS X users find frameworks automatically.<br />
<br />
* [[CMake:How To Build KDE4 Software | How to build KDE4 software with CMake]]<br />
<br />
* [[CMake:How To Process Lots Of Input Files | How to process lots of input files with a processor built by CMake]]<br />
<br />
* [http://qtnode.net/wiki?title=Qt_with_cmake Qt with CMake] <br>Explains how to use CMake to build software with Qt4, Qt3 and KDE3.<br />
<br />
* [http://www.wxwidgets.org/wiki/index.php/CMake How to use CMake for building software with wxWidgets ]<br />
<br />
* [http://www.linuxdevices.com/articles/AT6762290643.html Building eCos applications with CMake]<br />
<br />
* [[CMakeForFLTK| Using CMake to build an FLTK application]]<br />
<br />
* [[BuildingWinDLL| How to export symbols from a Windows DLL for the non-Windows Developer]]<br />
<br />
* [[BuildingOSXApplications| How to create a Stand Alone OS X Application bundle with Qt Support]]<br />
<br />
* [[VSConfigSpecificSettings| Configuration Specific Settings for Visual Studio Generated Project Files]]<br />
<br />
* [[BundleUtilitiesExample| How to use the 'BundleUtilities' to deploy your OS X Application]]<br />
<br />
* [[CMakeForFortranExample|How to write a simple CMakeLists.txt for Fortran code]]<br />
<br />
===Converters from other buildsystems to CMake===<br />
<br />
All converters listed here are not "complete", i.e. the generated CMake files are not 100% finished, in all cases some work is left for the developer.<br />
<br />
====automake/autotools/autoconf====<br />
* [http://websvn.kde.org/trunk/KDE/kdesdk/cmake/scripts/ am2cmake (requires Ruby) ] Converts automake/autotools/libtool based projects to CMake, specialized in converting from KDE 3 to KDE 4, should also work for others. This one has been used for converting the KDE buildsystem to CMake.<br />
<br />
* [http://emanuelgreisen.dk/stuff/kdevelop_am2cmake.php.tgz Alternative Automake2CMake (requires PHP)] Converts KDevelop projects that use automake to CMake.<br />
<br />
* [[GccXmlAutoConfHints|Converting autoconf tests]]<br />
<br />
====qmake====<br />
* [[CMake:ConvertFromQmake | qmake converter (requires Ruby)]] Converts projects that use Qt's qmake.<br />
<br />
<br />
====Visual Studio====<br />
* [http://www.eskilson.se/vcproj2cmake.rb vcproj2cmake.rb (requires Ruby)] Creates CMakeLists.txt files by extracting info from Visual Studio project files.<br />
<br />
====Basic CMakeLists.txt from-scratch-generator====<br />
* [http://websvn.kde.org/trunk/KDE/kdesdk/cmake/scripts/ gencmake (requires Ruby) ] Creates basic CMakeLists.txt files from looking at the existing files.<br />
<br />
===Success Stories===<br />
<br />
* What are some [[CMake Projects|projects using CMake]]?<br />
* [[CMake:Articles|Articles about CMake]]<br />
* [[Really Cool CMake Features]]<br />
<br />
===More Topics===<br />
<br />
* [[CMake Fortran Issues|Fortran Issues]]<br />
* [[CMake:For CMake Hackers|For CMake Hackers]]<br />
* [[CMake:Experiments With Lua|Experiments With Lua]]<br />
* [[CMake Performance Tips|Performance Tips]]<br />
* [[CMake:Install Commands| Replacing deprecated INSTALL_FILES, INSTALL_PROGRAMS and INSTALL_TARGETS commands]]<br />
* [[CMake:GNU style example | GNU style directory layout with CMake]]<br />
* [[CMake:OpenTasks| CMake TODO]]<br />
* [[CMake:CreateQtAssistantDocs| Creating Qt Assistant Docs]]<br />
<br />
==CTest==<br />
<br />
===Tutorials===<br />
* [[CMake Testing With CTest|Testing With CTest]]<br>Introduces to testing with CTest, submitting dashboards, and using CMake to add tests to the test system.<br />
<br />
* [[CMake Scripting Of CTest|CTest Scripting]]<br>Describes the scripting with CTest which can significantly simplify and automate testing and submitting dashboards.<br />
<br />
* [[CMake Generating Testing Files|Generating Input Files For CTest]]<br>Describe more in details the concepts behind testing with CTest and also explans how to use CTest without using CMake.<br />
<br />
===More Information===<br />
* [[CTest:Submission Issues|Configuring CTest Submission Methods]]<br />
* [[CTest:Nightly, Experimental, Continuous|CTest Nightly, Experimental, Continuous, ...]]<br />
* [[CTest:Coverage]]<br />
* [[Media:CTest Running Modes.pdf]]<br />
* [[CTest:FAQ|CTest Frequently asked questions]]<br />
<br />
===More Topics===<br />
* [[CTest:OpenTasks| CTest TODO]]<br />
<br />
==CPack==<br />
===Tutorials===<br />
* [[CMake:Packaging With CPack|Packaging with CPack]]<br>Introduction to CPack, installing and packaging of software.<br />
* [[CMake:CPackConfiguration|CPack Variables]]<br><br />
* [[CMake:CPackPackageGenerators|Supported package formats]]<br><br />
* [[CMake:CPackWin32NewbiesChecklist|CPack Win32 Newbie Checklist]] <br><br />
<br />
===More Information===<br />
* [[CPack:Generator Information|CPack Generator Information]]<br />
<br />
==CDash==<br />
* [[CDash|CDash Wiki]].<br />
* [[CDash:FAQ|CDash FAQ]].<br />
<br />
{{CMake/Template/Footer}}</div>Orbeckst