# HG changeset patch
# User Christoph Junghans <junghans@votca.org>
# Date 1326307444 25200
# Branch stable
# Node ID 475999c2cd0d19f739e83c88b2df823c32730bc1
# Parent  7e8f291a6d79da1ebbc512c370b8620714be0cb6
cmake: make it DSO conform

diff -r 7e8f291a6d79 -r 475999c2cd0d CMakeLists.txt
--- a/CMakeLists.txt	Tue Jan 10 20:16:55 2012 -0700
+++ b/CMakeLists.txt	Wed Jan 11 11:44:04 2012 -0700
@@ -4,9 +4,42 @@
 
 project(csgapps)
 
-file(GLOB_RECURSE DIRS */CMakeLists.txt)
+set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules)
+
+if(NOT CMAKE_BUILD_TYPE)
+  #Release comes with -O3 by default
+  set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel." FORCE)
+endif(NOT CMAKE_BUILD_TYPE)
+
+enable_language(CXX)
+include(CheckCXXCompilerFlag)
+
+option(EXTERNAL_BOOST "Use external boost" ON)
+if (EXTERNAL_BOOST)
+  find_package(Boost 1.39.0 COMPONENTS program_options )
+  if(NOT Boost_FOUND)
+    message(FATAL_ERROR "Boost not found,  make sure you have also installed boost and it's dev package or use our internal replacement, if you have built tools with -DEXTERNAL_BOOST=OFF and set it here as well")
+  endif(NOT Boost_FOUND)
+  include_directories(${Boost_INCLUDE_DIRS})
+  set (BOOST_LIBRARIES ${Boost_PROGRAM_OPTIONS_LIBRARY})
+else(EXTERNAL_BOOST)
+  find_package(VOTCA_BOOST REQUIRED)
+  include_directories(${VOTCA_BOOST_INCLUDE_DIRS})
+  set (BOOST_LIBRARIES ${VOTCA_BOOST_LIBRARIES})
+endif(EXTERNAL_BOOST)
+
+find_package(VOTCA_TOOLS REQUIRED)
+include_directories(${VOTCA_TOOLS_INCLUDE_DIRS})
+find_package(VOTCA_CSG REQUIRED)
+include_directories(${VOTCA_CSG_INCLUDE_DIRS})
+
+file(GLOB_RECURSE DIRS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} */CMakeLists.txt)
 foreach(DIR ${DIRS})
-  string(REGEX MATCH ^.*/ DIR ${DIR})
-  add_subdirectory(${DIR})
-  unset(NAME CACHE)
+  string(REGEX MATCH ^[^/]* DIR ${DIR})
+  file(GLOB SRC ${DIR}/*.cc)
+  add_executable(${DIR} ${SRC})
+  target_link_libraries(${DIR} ${VOTCA_CSG_LIBRARIES} ${VOTCA_TOOLS_LIBRARIES} ${BOOST_LIBRARIES})
+  install(TARGETS ${DIR} RUNTIME DESTINATION bin)
+  file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${DIR})
+  set_target_properties(${DIR} PROPERTIES OUTPUT_NAME ${DIR}/csg_${DIR})
 endforeach(DIR)
diff -r 7e8f291a6d79 -r 475999c2cd0d CMakeModules/FindSQLITE3.cmake
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CMakeModules/FindSQLITE3.cmake	Wed Jan 11 11:44:04 2012 -0700
@@ -0,0 +1,36 @@
+# - Find libgmx
+# Find the native libgmx headers and libraries.
+#
+#  SQLITE3_INCLUDE_DIRS - where to find sqlite3.h, etc
+#  SQLITE3_LIBRARIES    - List of libraries when using sqlite3.
+#  SQLITE3_FOUND        - True if sqlite3 found.
+#
+# Copyright 2009-2011 The VOTCA Development Team (http://www.votca.org)
+#
+# 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)
+
+pkg_check_modules(PC_SQLITE3 sqlite3)
+
+find_path(SQLITE3_INCLUDE_DIR sqlite3.h HINTS ${PC_SQLITE3_INCLUDE_DIRS})
+find_library(SQLITE3_LIBRARY NAMES sqlite3 HINTS ${PC_SQLITE3_LIBRARY_DIRS} )
+
+set(SQLITE3_LIBRARIES "${SQLITE3_LIBRARY}" )
+set(SQLITE3_INCLUDE_DIRS "${SQLITE3_INCLUDE_DIR}" )
+
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(SQLITE3 DEFAULT_MSG SQLITE3_LIBRARY SQLITE3_INCLUDE_DIR )
+
+mark_as_advanced(SQLITE3_INCLUDE_DIR SQLITE3_LIBRARY )
diff -r 7e8f291a6d79 -r 475999c2cd0d CMakeModules/FindVOTCA_BOOST.cmake
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CMakeModules/FindVOTCA_BOOST.cmake	Wed Jan 11 11:44:04 2012 -0700
@@ -0,0 +1,37 @@
+# - Find libvotca_boost
+# Find the native libvotca_boost headers and libraries.
+#
+#  VOTCA_BOOST_INCLUDE_DIRS - where to find headers etc.
+#  VOTCA_BOOST_LIBRARIES    - List of libraries when using expat.
+#  VOTCA_BOOST_FOUND        - True if expat found.
+#
+# Copyright 2009-2011 The VOTCA Development Team (http://www.votca.org)
+#
+# 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)
+
+pkg_check_modules(PC_VOTCA_BOOST libvotca_boost)
+find_path(VOTCA_BOOST_INCLUDE_DIR boost/algorithm/string/trim.hpp HINTS ${PC_VOTCA_BOOST_INCLUDE_DIRS} )
+find_library(VOTCA_BOOST_LIBRARY NAMES votca_boost HINTS ${PC_VOTCA_BOOST_LIBRARY_DIRS} )
+
+set(VOTCA_BOOST_LIBRARIES "${VOTCA_BOOST_LIBRARY}" )
+set(VOTCA_BOOST_INCLUDE_DIRS "${VOTCA_BOOST_INCLUDE_DIR}" )
+
+include(FindPackageHandleStandardArgs)
+# handle the QUIETLY and REQUIRED arguments and set VOTCA_BOOST_FOUND to TRUE
+# if all listed variables are TRUE
+find_package_handle_standard_args(VOTCA_BOOST DEFAULT_MSG VOTCA_BOOST_LIBRARY VOTCA_BOOST_INCLUDE_DIR)
+
+mark_as_advanced(VOTCA_BOOST_INCLUDE_DIR VOTCA_BOOST_LIBRARY )
diff -r 7e8f291a6d79 -r 475999c2cd0d CMakeModules/FindVOTCA_CSG.cmake
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CMakeModules/FindVOTCA_CSG.cmake	Wed Jan 11 11:44:04 2012 -0700
@@ -0,0 +1,51 @@
+# - Find libvotca_csg
+# Find the native libvotca_csg headers and libraries.
+#
+#  VOTCA_CSG_INCLUDE_DIRS - where to find votca/csg/version.h, etc.
+#  VOTCA_CSG_LIBRARIES    - List of libraries when using expat.
+#  VOTCA_CSG_FOUND        - True if expat found.
+#  VOTCA_CSG_HAS_SQLITE3  - True if votca csg was build with sqlite3 support
+#
+# Copyright 2009-2011 The VOTCA Development Team (http://www.votca.org)
+#
+# 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)
+
+pkg_check_modules(PC_VOTCA_CSG libvotca_csg)
+find_path(VOTCA_CSG_INCLUDE_DIR votca/csg/version.h HINTS ${PC_VOTCA_CSG_INCLUDE_DIRS})
+
+find_library(VOTCA_CSG_LIBRARY NAMES votca_csg HINTS ${PC_VOTCA_CSG_LIBRARY_DIRS} )
+
+if("${VOTCA_CSG_LIBRARY}" MATCHES "libvotca_csg[^;]*\\.a")
+    if(PC_VOTCA_CSG_LIBRARIES)
+      list(REMOVE_ITEM PC_VOTCA_CSG_LIBRARIES votca_csg)
+      foreach (LIB ${PC_VOTCA_CSG_LIBRARIES})
+        find_library(VOTCA_CSG_${LIB} NAMES ${LIB} HINTS ${PC_VOTCA_CSG_LIBRARY_DIRS} )
+        list(APPEND VT_DEP_LIBRARIES ${VOTCA_CSG_${LIB}})
+        unset(VOTCA_CSG_${LIB} CACHE)
+      endforeach(LIB)
+    endif(PC_VOTCA_CSG_LIBRARIES)
+    set(VOTCA_CSG_DEP_LIBRARIES "${VT_DEP_LIBRARIES}" CACHE FILEPATH "votca csg depency libs (only needed for static (.a) libvotca_csg")
+endif("${VOTCA_CSG_LIBRARY}" MATCHES "libvotca_csg[^;]*\\.a")
+
+set(VOTCA_CSG_LIBRARIES "${VOTCA_CSG_LIBRARY};${VOTCA_CSG_DEP_LIBRARIES}" )
+set(VOTCA_CSG_INCLUDE_DIRS "${VOTCA_CSG_INCLUDE_DIR}" )
+
+include(FindPackageHandleStandardArgs)
+# handle the QUIETLY and REQUIRED arguments and set VOTCA_CSG_FOUND to TRUE
+# if all listed variables are TRUE
+find_package_handle_standard_args(VOTCA_CSG DEFAULT_MSG VOTCA_CSG_LIBRARY VOTCA_CSG_INCLUDE_DIR )
+
+mark_as_advanced(VOTCA_CSG_INCLUDE_DIR VOTCA_CSG_LIBRARY )
diff -r 7e8f291a6d79 -r 475999c2cd0d CMakeModules/FindVOTCA_TOOLS.cmake
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CMakeModules/FindVOTCA_TOOLS.cmake	Wed Jan 11 11:44:04 2012 -0700
@@ -0,0 +1,68 @@
+# - Find libvotca_tools
+# Find the native libvotca_tools headers and libraries.
+#
+#  VOTCA_TOOLS_INCLUDE_DIRS - where to find votca/tools/version.h, etc.
+#  VOTCA_TOOLS_LIBRARIES    - List of libraries when using expat.
+#  VOTCA_TOOLS_FOUND        - True if expat found.
+#  VOTCA_TOOLS_HAS_SQLITE3  - True if votca tools was build with sqlite3 support
+#
+# Copyright 2009-2011 The VOTCA Development Team (http://www.votca.org)
+#
+# 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)
+
+pkg_check_modules(PC_VOTCA_TOOLS libvotca_tools)
+find_path(VOTCA_TOOLS_INCLUDE_DIR votca/tools/version.h HINTS ${PC_VOTCA_TOOLS_INCLUDE_DIRS})
+
+find_path(VOTCA_TOOLS_HAS_SQLITE3 votca/tools/database.h HINTS ${PC_VOTCA_TOOLS_INCLUDE_DIRS})
+if (VOTCA_TOOLS_HAS_SQLITE3)
+  #due to include <sqlite3.h> in database.h
+  find_package(SQLITE3 REQUIRED)
+  set(VOTCA_TOOLS_INCLUDE_DIRS "${VOTCA_TOOLS_INCLUDE_DIR};${SQLITE3_INCLUDE_DIR}" )
+else(VOTCA_TOOLS_HAS_SQLITE3)
+  set(VOTCA_TOOLS_INCLUDE_DIRS "${VOTCA_TOOLS_INCLUDE_DIR}" )
+endif (VOTCA_TOOLS_HAS_SQLITE3)
+
+
+find_library(VOTCA_TOOLS_LIBRARY NAMES votca_tools HINTS ${PC_VOTCA_TOOLS_LIBRARY_DIRS} )
+
+if("${VOTCA_TOOLS_LIBRARY}" MATCHES "libvotca_tools[^;]*\\.a")
+    if(PC_VOTCA_TOOLS_LIBRARIES)
+      list(REMOVE_ITEM PC_VOTCA_TOOLS_LIBRARIES votca_tools)
+      foreach (LIB ${PC_VOTCA_TOOLS_LIBRARIES})
+        find_library(VOTCA_TOOLS_${LIB} NAMES ${LIB} HINTS ${PC_VOTCA_TOOLS_LIBRARY_DIRS} )
+        list(APPEND VT_DEP_LIBRARIES ${VOTCA_TOOLS_${LIB}})
+        unset(VOTCA_TOOLS_${LIB} CACHE)
+      endforeach(LIB)
+    endif(PC_VOTCA_TOOLS_LIBRARIES)
+    set(VOTCA_TOOLS_DEP_LIBRARIES "${VT_DEP_LIBRARIES}" CACHE FILEPATH "votca tools depency libs (only needed for static (.a) libvotca_tools")
+endif("${VOTCA_TOOLS_LIBRARY}" MATCHES "libvotca_tools[^;]*\\.a")
+
+set(VOTCA_TOOLS_LIBRARIES "${VOTCA_TOOLS_LIBRARY};${VOTCA_TOOLS_DEP_LIBRARIES}" )
+
+include(FindPackageHandleStandardArgs)
+# handle the QUIETLY and REQUIRED arguments and set VOTCA_TOOLS_FOUND to TRUE
+# if all listed variables are TRUE
+find_package_handle_standard_args(VOTCA_TOOLS DEFAULT_MSG VOTCA_TOOLS_LIBRARY VOTCA_TOOLS_INCLUDE_DIR )
+
+if (VOTCA_TOOLS_FOUND)
+  include(CheckLibraryExists)
+  check_library_exists("${VOTCA_TOOLS_LIBRARY};${VOTCA_TOOLS_DEP_LIBRARIES}" VotcaToolsFromC "" FOUND_VOTCA_TOOLS_VERSION)
+  if(NOT FOUND_VOTCA_TOOLS_VERSION)
+    message(FATAL_ERROR "Could not find VotcaToolsFromC in ${VOTCA_TOOLS_LIBRARY};${VOTCA_TOOLS_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 VOTCA_TOOLS_LIBRARY and VOTCA_TOOLS_DEP_LIBRARIES by hand, which set votca_tools lib  it's depencies (i.e. -DVOTCA_TOOLS_LIBRARY='/path/to/libvotca_tools.so" -VOTCA_TOOLS_DEP_LIBRARIES="/path/to/libgsl.so;/path/to/libm.so') !")
+  endif(NOT FOUND_VOTCA_TOOLS_VERSION)
+endif (VOTCA_TOOLS_FOUND)
+
+mark_as_advanced(VOTCA_TOOLS_INCLUDE_DIR VOTCA_TOOLS_LIBRARY )