Add FindGROMACS.cmake to template's CMakeLists.txt
authorChristoph Junghans <junghans@votca.org>
Fri, 29 Jun 2012 20:18:17 +0000 (14:18 -0600)
committerChristoph Junghans <junghans@votca.org>
Fri, 29 Jun 2012 20:18:17 +0000 (14:18 -0600)
Change-Id: Id5adec8aa057f1d56021b06aaedd35dab108ead7

share/CMakeLists.txt
share/template/CMakeLists.txt [new file with mode: 0644]
share/template/CMakeLists.txt.template
share/template/Makefile.pkg
share/template/README
share/template/cmake/FindGROMACS.cmake [new file with mode: 0644]

index 0fd88a476b73b74c2cc1cb7f06d6e661f4f9466b..66d05c1ecb964cadfad954355555b6cb9534a784 100644 (file)
@@ -4,12 +4,8 @@ install(DIRECTORY . DESTINATION ${DATA_INSTALL_DIR}
   PATTERN "Makefile*" EXCLUDE
   PATTERN "CMake*" EXCLUDE
   PATTERN "cmake*" EXCLUDE
-  PATTERN "template_doc.c" EXCLUDE
-  PATTERN "Template.mak" EXCLUDE
   PATTERN "*~" EXCLUDE
+  PATTERN "template" EXCLUDE
 )
 
-install(FILES template/CMakeLists.txt.template
-        DESTINATION ${DATA_INSTALL_DIR}
-        RENAME template/CMakeLists.txt
-        COMPONENT data)
+add_subdirectory(template)
diff --git a/share/template/CMakeLists.txt b/share/template/CMakeLists.txt
new file mode 100644 (file)
index 0000000..00d6be7
--- /dev/null
@@ -0,0 +1,13 @@
+install(FILES CMakeLists.txt.template
+        DESTINATION ${DATA_INSTALL_DIR}/template
+        RENAME CMakeLists.txt
+        COMPONENT development)
+
+install(FILES README template.c Makefile.pkg
+        DESTINATION ${DATA_INSTALL_DIR}/template
+        COMPONENT development)
+
+install(FILES cmake/FindGROMACS.cmake
+        DESTINATION ${DATA_INSTALL_DIR}/template/cmake
+        COMPONENT development)
+
index a23683c9ebacbd3719de6b255c32223ec27f3669..10a6a707775c0022825ffeffd070025b898fb49f 100644 (file)
@@ -9,7 +9,6 @@ if(NOT CMAKE_BUILD_TYPE)
     set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
 endif(NOT CMAKE_BUILD_TYPE)
 
-
 enable_language(C)
 
 option(GMX_DOUBLE "Use double precision" OFF)
@@ -29,22 +28,18 @@ ENDIF(CMAKE_GENERATOR MATCHES "Visual Studio")
 ########################################################################
 
 if (GMX_DOUBLE)
-  set(LIBMD "libmd_d")
+  set(LIBGROMACS "libgmx_d")
 else(GMX_DOUBLE)
-  set(LIBMD "libmd")
+  set(LIBGROMACS "libgmx")
 endif(GMX_DOUBLE)
 
-#only check for libmd, because it depends on libgmx
-FIND_PACKAGE(PkgConfig)
-#do not use REQUIRED here, we have a better error message then pkgconfig
-PKG_CHECK_MODULES(LIBMD ${LIBMD})
-if (NOT LIBMD_FOUND)
-  message(FATAL_ERROR "${LIBMD} not found, source GMXRC.")
-endif (NOT LIBMD_FOUND)
-add_definitions( ${LIBMD_CFLAGS} )
-include_directories( ${LIBMD_INCLUDE_DIRS} )
-link_directories( ${LIBMD_LIBRARY_DIRS} )
+FIND_PACKAGE(GROMACS COMPONENTS ${LIBGROMACS} REQUIRED)
+if ("${GROMACS_VERSION_STRING}" VERSION_LESS "4.6" OR "${GROMACS_VERSION_STRING}" VERSION_GREATER "4.99")
+  message(FATAL_ERROR "This template works only with GROMACS 4.6")
+endif ("${GROMACS_VERSION_STRING}" VERSION_LESS "4.6" OR "${GROMACS_VERSION_STRING}" VERSION_GREATER "4.99")
 
+add_definitions( ${GROMACS_DEFINITIONS} )
+include_directories( ${GROMACS_INCLUDE_DIRS} )
 
 ########################################################################
 # Specify install locations and which subdirectories to process        #
@@ -52,4 +47,4 @@ link_directories( ${LIBMD_LIBRARY_DIRS} )
 set(BIN_INSTALL_DIR  ${CMAKE_INSTALL_PREFIX}/bin)
 
 add_executable(template template.c)
-target_link_libraries(template ${LIBMD_LIBRARIES})
+target_link_libraries(template ${GROMACS_LIBRARIES})
index 140251bf9bd15f79eca30ddb7ad6838738ca1537..e4788c4289900e52eb29df73dd8850d18aa651c0 100644 (file)
@@ -2,6 +2,10 @@
 #
 # Copy this file to whatever directory you are using for your own software
 #
+# Usage:
+# $ source /path/to/GMXRC
+# $ make -f Makefile.pkg
+#
 #change the name of the program here
 NAME=template
 
@@ -15,7 +19,7 @@ EXTRA_SRC=
 #what should be done by default
 all: $(NAME)
 
-#if VOTCALDLIB is defined we add it to PKG_CONFIG_PATH
+#if GMXLDLIB is defined we add it to PKG_CONFIG_PATH
 ifeq "$(origin GMXLDLIB)" "undefined"
   $(error "GMXLDLIB not found, please source GMXRC")
 else
@@ -23,8 +27,8 @@ else
 endif
 
 #get CPPFLAGS and LDFLAGS from pkg-config
-CPPFLAGS=`pkg-config --cflags libmd`
-LDFLAGS=`pkg-config --libs libmd`
+CPPFLAGS=`pkg-config --cflags libgmx`
+LDFLAGS=`pkg-config --libs libgmx`
 
 #generate a list of object (.o) files
 OBJS=$(patsubst %.c,%.o,$(NAME).c $(EXTRA_SRC))
index eb6e841a47b5b9e4ddbbcacb5086e785f2496dbb..535db079bcb0640ff7f2bf1c33088482e98dd063 100644 (file)
@@ -1,24 +1,19 @@
-Once installed, this directory contains a Makefile and 
-a small program that you could use as a template when 
-writing your own analysis software.
+Once installed, this directory contains CMakelist.txt,
+Makefile.pkg and a small program that you could use as
+a template when writing your own analysis software.
 
-A Makefile.arch is created for each architecture you install 
-with the correct paths and libraries. You will have to link
-(or copy) the correct makefile to "Makefile" or use the -f 
-option to gmake in order to select a makefile.
+The CMakelist.txt can be used together with cmake to
+build the template program:
+$ source /path/to/GMXRC
+$ cmake /path/to/template
 
-----------------------------------------------------------
-
-If you are reading this in the distribution directory, 
-there will be a Makefile.am which automake uses to 
-create a Makefile.in which in turn is converted to
-a Makefile by the configure script. Note that this is
-NOT the template makefile - it is called Template.mak,
-and used to create Makefile.arch at build time.
+The Makefile.pkg can be used if cmake is unavailble. It
+makes use of pkg-config only to build the template program:
+$ source /path/to/GMXRC
+$ make -f Makefile.pkg
 
-You should NOT touch or copy Makefile.in unless you
-really know what you are doing - it is a template file
-used by the configure script to create the real Makefile.
+----------------------------------------------------------
 
-If you want to use automake and autoconf yourself, look
-in the contrib directory of the distribution.
+If you are reading this in the distribution directory,
+remember that CMakelist.txt.template will be renamed to
+CMakelist.txt during the installation.
diff --git a/share/template/cmake/FindGROMACS.cmake b/share/template/cmake/FindGROMACS.cmake
new file mode 100644 (file)
index 0000000..7014ee6
--- /dev/null
@@ -0,0 +1,132 @@
+# - Finds parts of GROMACS
+# Find the native GROMACS compents headers and libraries.
+#
+#  GROMACS_INCLUDE_DIRS   - where to find GROMACS headers.
+#  GROMACS_LIBRARIES      - List of libraries when used by GROMACS.
+#  GROMACS_FOUND          - True if all GROMACS components were found.
+#  GROMACS_DEFINITIONS    - Extra definies needed by GROMACS
+#  GROMACS_PKG            - The name of the pkg-config package needed
+#  GROMACS_VERSION        - GROMACS lib interface version
+#  GROMACS_MAJOR_VERSION  - GROMACS lib interface major version
+#  GROMACS_MINOR_VERSION  - GROMACS lib interface minor version
+#  GROMACS_PATCH_LEVEL    - GROMACS lib interface patch level
+#  GROMACS_VERSION_STRING - GROMACS lib interface version string (e.g. "4.5.3")
+#
+# Copyright 2009-2011 The VOTCA Development Team (http://www.votca.org)
+# Copyright 2012 The Gromacs Team
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+find_package(PkgConfig)
+list(LENGTH GROMACS_FIND_COMPONENTS GROMACS_NUM_COMPONENTS_WANTED)
+if(${GROMACS_NUM_COMPONENTS_WANTED} LESS 1)
+  message(FATAL_ERROR "find_package(GROMACS) needs to be supplied with the name of a GROMACS component for which it can search")
+elseif(${GROMACS_NUM_COMPONENTS_WANTED} GREATER 1)
+  message(FATAL_ERROR "We only support finding one GROMACS component at this point, go and implement it ;-)")
+elseif(${GROMACS_FIND_COMPONENTS} MATCHES "^lib(gmx|gromacs)(_d)?$")
+  set(GROMACS_PKG "${GROMACS_FIND_COMPONENTS}")
+  string(REGEX REPLACE "^lib(.*)" "\\1" GROMACS_LIBRARY_NAME "${GROMACS_PKG}")
+else()
+  message(FATAL_ERROR "We do not support finding ${GROMACS_FIND_COMPONENTS}, go and implement it ;-)")
+endif()
+
+if(GMX_DOUBLE AND NOT "${GROMACS_PKG}" MATCHES "_d$")
+  message(FATAL_ERROR "GMX_DOUBLE was true, but I was asked to find ${GROMACS_PKG} (without _d at the end) - illogical!")
+endif(GMX_DOUBLE AND NOT "${GROMACS_PKG}" MATCHES "_d$")
+if(NOT GMX_DOUBLE AND "${GROMACS_PKG}" MATCHES "_d$")
+  message(FATAL_ERROR "GMX_DOUBLE was false, but I was asked to find ${GROMACS_PKG} (with _d at the end) - illogical!")
+endif(NOT GMX_DOUBLE AND "${GROMACS_PKG}" MATCHES "_d$")
+
+pkg_check_modules(PC_GROMACS ${GROMACS_PKG})
+if (GMX_DOUBLE)
+  list(APPEND GMX_DEFS "-DGMX_DOUBLE")
+endif(GMX_DOUBLE)
+if (PC_GROMACS_CFLAGS_OTHER)
+  foreach(DEF ${PC_GROMACS_CFLAGS_OTHER})
+    if (${DEF} MATCHES "^-D")
+      list(APPEND GMX_DEFS ${DEF})
+    endif (${DEF} MATCHES "^-D")
+  endforeach(DEF)
+  list(REMOVE_DUPLICATES GMX_DEFS)
+endif (PC_GROMACS_CFLAGS_OTHER)
+set(GROMACS_DEFINITIONS "${GMX_DEFS}" CACHE STRING "extra GROMACS definitions")
+
+find_library(GROMACS_LIBRARY NAMES ${GROMACS_LIBRARY_NAME} HINTS ${PC_GROMACS_LIBRARY_DIRS} )
+if (GROMACS_LIBRARY)
+  if("${GROMACS_LIBRARY}" MATCHES "lib(gmx|gromacs)[^;]*\\.a")
+    if(PC_GROMACS_LIBRARIES)
+      list(REMOVE_ITEM PC_GROMACS_LIBRARIES ${GROMACS_LIBRARY_NAME})
+      foreach (LIB ${PC_GROMACS_LIBRARIES})
+        find_library(GROMACS_${LIB} NAMES ${LIB} HINTS ${PC_GROMACS_LIBRARY_DIRS} )
+        list(APPEND GMX_DEP_LIBRARIES ${GROMACS_${LIB}})
+        unset(GROMACS_${LIB} CACHE)
+      endforeach(LIB)
+    endif(PC_GROMACS_LIBRARIES)
+    if(PC_GROMACS_CFLAGS_OTHER)
+      foreach(LIB ${PC_GROMACS_CFLAGS_OTHER})
+        if (${LIB} MATCHES "thread")
+          find_package(Threads REQUIRED)
+          list(APPEND GMX_DEP_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
+        endif (${LIB} MATCHES "thread")
+      endforeach(LIB)
+    endif(PC_GROMACS_CFLAGS_OTHER)
+    set(GROMACS_DEP_LIBRARIES "${GMX_DEP_LIBRARIES}" CACHE FILEPATH "GROMACS depency libs (only needed for static (.a) ${GROMACS_LIBRARY}")
+  endif("${GROMACS_LIBRARY}" MATCHES "lib(gmx|gromacs)[^;]*\\.a")
+  include(CheckLibraryExists)
+  check_library_exists("${GROMACS_LIBRARY};${GROMACS_DEP_LIBRARIES}" GromacsVersion "" FOUND_GROMACS_VERSION)
+  if(NOT FOUND_GROMACS_VERSION)
+    message(FATAL_ERROR "Could not find GromacsVersion in ${GROMACS_LIBRARY};${GROMACS_DEP_LIBRARIES}, take look at the error message in ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log to find out what was going wrong. If you don't have pkg-config installed you will most likely have to set GROMACS_LIBRARY and GROMACS_DEP_LIBRARY by hand which sets the GROMACS lib and its depencies (e.g. -DGROMACS_LIBRARY='/path/to/libgmx.so' -DGROMACS_DEP_LIBRARIES='/path/to/libblas.so;/path/to/libm.so') !")
+  endif(NOT FOUND_GROMACS_VERSION)
+  check_library_exists("${GROMACS_LIBRARY};${GROMACS_DEP_LIBRARIES}" init_mtop "" FOUND_GROMACS_INIT_MTOP)
+  if(NOT FOUND_GROMACS_INIT_MTOP)
+    message(FATAL_ERROR "Could not find init_mtop in the GROMACS library, take look at the error message in ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log to find out what was going wrong. This most likely means that your GROMACS version is too old, we need at least GROMACS 4.0.7.")
+  endif(NOT FOUND_GROMACS_INIT_MTOP)
+  set(GROMACS_VERSION 40000)
+  check_library_exists("${GROMACS_LIBRARY};${GROMACS_DEP_LIBRARIES}" output_env_init "" FOUND_GROMACS_OUTPUT_ENV_INIT)
+  if(FOUND_GROMACS_OUTPUT_ENV_INIT)
+    set(GROMACS_VERSION 40500)
+  endif(FOUND_GROMACS_OUTPUT_ENV_INIT)
+  check_library_exists("${GROMACS_LIBRARY};${GROMACS_DEP_LIBRARIES}" init_domdec_vsites "" FOUND_GROMACS_INIT_DOMDEC_VSITES)
+  if(FOUND_GROMACS_INIT_DOMDEC_VSITES)
+    set(GROMACS_VERSION 50000)
+  endif(FOUND_GROMACS_INIT_DOMDEC_VSITES)
+  set(GROMACS_VERSION ${GROMACS_VERSION} CACHE STRING "GROMACS lib interface version")
+else(GROMACS_LIBRARY)
+  set(GROMACS_VERSION 40500)
+endif (GROMACS_LIBRARY)
+
+math(EXPR GROMACS_MAJOR_VERSION "${GROMACS_VERSION} / 10000")
+math(EXPR GROMACS_MINOR_VERSION "${GROMACS_VERSION} / 100 % 100")
+math(EXPR GROMACS_PATCH_LEVEL "${GROMACS_VERSION} % 100")
+set(GROMACS_VERSION_STRING "${GROMACS_MAJOR_VERSION}.${GROMACS_MINOR_VERSION}.${GROMACS_PATCH_LEVEL}")
+
+if ("${GROMACS_PKG}" MATCHES "libgmx")
+  if (${GROMACS_VERSION} EQUAL 40000)
+    find_path(GROMACS_INCLUDE_DIR tpxio.h HINTS ${PC_GROMACS_INCLUDE_DIRS})
+  else(${GROMACS_VERSION} EQUAL 40000)
+   find_path(GROMACS_INCLUDE_DIR gromacs/tpxio.h HINTS ${PC_GROMACS_INCLUDE_DIRS})
+  endif(${GROMACS_VERSION} EQUAL 40000)
+elseif("${GROMACS_PKG}" MATCHES "libgromacs")
+  find_path(GROMACS_INCLUDE_DIR gromacs/legacyheaders/tpxio.h HINTS ${PC_GROMACS_INCLUDE_DIRS})
+endif("${GROMACS_PKG}" MATCHES "libgmx")
+
+set(GROMACS_LIBRARIES "${GROMACS_LIBRARY};${GROMACS_DEP_LIBRARIES}" )
+set(GROMACS_INCLUDE_DIRS ${GROMACS_INCLUDE_DIR} )
+
+include(FindPackageHandleStandardArgs)
+# handle the QUIETLY and REQUIRED arguments and set GROMACS_FOUND to TRUE
+# if all listed variables are TRUE
+find_package_handle_standard_args(GROMACS DEFAULT_MSG GROMACS_LIBRARY GROMACS_INCLUDE_DIR)
+
+mark_as_advanced(GROMACS_INCLUDE_DIR GROMACS_LIBRARY GROMACS_DEFINITIONS GROMACS_PKG GROMACS_VERSION GROMACS_DEP_LIBRARIES)