From feafe442a6f0cb88cf3a22c95172eb79fbca6730 Mon Sep 17 00:00:00 2001
From: Whitney Armstrong <warmstrong@anl.gov>
Date: Fri, 25 Oct 2019 19:01:47 -0500
Subject: [PATCH] Added Sylvester's modulefile/singularity build. Works like a
 charm.

	new file:   CMakeLists.txt
	new file:   cmake/options.cmake
	new file:   containers/CMakeLists.txt
	new file:   modulefiles/CMakeLists.txt
	new file:   modulefiles/modulefile.in
	new file:   scripts/CMakeLists.txt
	new file:   scripts/launcher.in
---
 CMakeLists.txt             | 21 +++++++++++++++++++++
 cmake/options.cmake        | 34 ++++++++++++++++++++++++++++++++++
 containers/CMakeLists.txt  | 24 ++++++++++++++++++++++++
 modulefiles/CMakeLists.txt | 11 +++++++++++
 modulefiles/modulefile.in  | 14 ++++++++++++++
 scripts/CMakeLists.txt     | 29 +++++++++++++++++++++++++++++
 scripts/launcher.in        | 16 ++++++++++++++++
 7 files changed, 149 insertions(+)
 create mode 100644 CMakeLists.txt
 create mode 100644 cmake/options.cmake
 create mode 100644 containers/CMakeLists.txt
 create mode 100644 modulefiles/CMakeLists.txt
 create mode 100644 modulefiles/modulefile.in
 create mode 100644 scripts/CMakeLists.txt
 create mode 100755 scripts/launcher.in

diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..1e31763
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,21 @@
+cmake_minimum_required (VERSION 3.8)
+project (freecad_image
+  VERSION 0.9)
+
+################################################################################
+## The name of our container
+################################################################################
+set(CONTAINER freecad-${PROJECT_VERSION}.sif)
+
+################################################################################
+## CMAKE Settings 
+################################################################################
+include(cmake/options.cmake)
+
+################################################################################
+## Subdirectories
+################################################################################
+
+add_subdirectory(containers)
+add_subdirectory(modulefiles)
+add_subdirectory(scripts)
diff --git a/cmake/options.cmake b/cmake/options.cmake
new file mode 100644
index 0000000..facd4c2
--- /dev/null
+++ b/cmake/options.cmake
@@ -0,0 +1,34 @@
+################################################################################
+## CMAKE Settings 
+################################################################################
+## make sure that the default is RELEASE
+if (NOT CMAKE_BUILD_TYPE)
+  set (CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING
+      "Choose the type of build, options are: None Debug Release RelWithDebInfo."
+      FORCE)
+endif ()
+## Offer the user the choice of overriding the installation directories
+set(INSTALL_LIB_DIR lib CACHE PATH "Installation directory for libraries")
+set(INSTALL_BIN_DIR bin CACHE PATH "Installation directory for executables")
+set(INSTALL_INCLUDE_DIR include/${PROJECT_NAME} CACHE PATH "Installation directory for header files")
+set(INSTALL_MODULE_DIR ${CMAKE_INSTALL_PREFIX}/../../etc/modulefiles CACHE PATH "Installation directory for module files")
+set(INSTALL_BIND_PATH "" CACHE PATH "Installation singularity bind path")
+if(WIN32 AND NOT CYGWIN)
+  set(DEF_INSTALL_CMAKE_DIR cmake)
+else()
+  set(DEF_INSTALL_CMAKE_DIR lib/cmake/${PROJECT_NAME})
+endif()
+set(INSTALL_CMAKE_DIR ${DEF_INSTALL_CMAKE_DIR} CACHE PATH
+  "Installation directory for cmake files")
+## Make relative paths absolute (useful when auto-generating cmake configuration
+## files) Note: not including INSTALL_BIND_PATH on purpose as we should be giving absolute
+## paths anyway
+foreach(p LIB BIN INCLUDE CMAKE MODULE)
+  set(var INSTALL_${p}_DIR)
+  if(NOT IS_ABSOLUTE "${${var}}")
+    set(${var} "${CMAKE_INSTALL_PREFIX}/${${var}}")
+  endif()
+endforeach()
+## extra cmake modules
+set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} 
+  "${PROJECT_SOURCE_DIR}/cmake/")
diff --git a/containers/CMakeLists.txt b/containers/CMakeLists.txt
new file mode 100644
index 0000000..0d6f820
--- /dev/null
+++ b/containers/CMakeLists.txt
@@ -0,0 +1,24 @@
+################################################################################
+## Download the container
+################################################################################
+
+set(CONTAINER_SOURCE
+  "https://eicweb.phy.anl.gov/containers/freecad_image/-/jobs/artifacts/master/raw/build/Singularity.freecad.simg?job=freecad_image_singularity")
+  #"https://eicweb.phy.anl.gov/containers/freecad_image/-/jobs/artifacts/v${PROJECT_VERSION}/raw/build/freecad.sif?job=freecad_image_singularity")
+
+add_custom_command(OUTPUT ${CONTAINER} 
+  COMMAND wget ${CONTAINER_SOURCE} -O ${CONTAINER}
+  COMMENT "Downloading ${CONTAINER}"
+)
+
+################################################################################
+## Global target
+################################################################################
+add_custom_target(container ALL 
+  DEPENDS ${CONTAINER})
+
+################################################################################
+## Install the container to <prefix>/lib
+################################################################################
+install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${CONTAINER}
+  DESTINATION "${INSTALL_LIB_DIR}" COMPONENT lib)
diff --git a/modulefiles/CMakeLists.txt b/modulefiles/CMakeLists.txt
new file mode 100644
index 0000000..d7a8005
--- /dev/null
+++ b/modulefiles/CMakeLists.txt
@@ -0,0 +1,11 @@
+################################################################################
+## Configure modulefile for our environment
+################################################################################
+set(MODULEFILE "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}")
+configure_file(modulefile.in ${MODULEFILE} @ONLY)
+
+################################################################################
+## Install the modulefile to <install_module_dir>/<project>/<version>
+################################################################################
+install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${MODULEFILE}
+  DESTINATION "${INSTALL_MODULE_DIR}/${PROJECT_NAME}" COMPONENT lib)
diff --git a/modulefiles/modulefile.in b/modulefiles/modulefile.in
new file mode 100644
index 0000000..c1aeb28
--- /dev/null
+++ b/modulefiles/modulefile.in
@@ -0,0 +1,14 @@
+#%Module1.0#####################################################################
+##
+## for tmux
+##
+proc ModulesHelp { } {
+    puts stderr "This module sets up the environment for the Hall A/C container"
+}
+module-whatis   "@PROJECT_NAME@ @PROJECT_VERSION_MAJOR@.@PROJECT_VERSION_MINOR@"
+
+# for Tcl script use only
+set    version        4.1.4
+set iprefix @CMAKE_INSTALL_PREFIX@
+
+prepend-path    PATH             $iprefix/bin
diff --git a/scripts/CMakeLists.txt b/scripts/CMakeLists.txt
new file mode 100644
index 0000000..fdf3212
--- /dev/null
+++ b/scripts/CMakeLists.txt
@@ -0,0 +1,29 @@
+################################################################################
+## Configure launcher scripts for our singularity apps
+################################################################################
+
+set(APPS 
+  "FreeCAD"
+  )
+
+set(APPS_ABSOLUTE_PATH "")
+
+## Bind directive for known systems
+set (OPT_BIND_DIRECTIVE "")
+if (NOT "${INSTALL_BIND_PATH} " EQUAL " " AND EXISTS ${INSTALL_BIND_PATH})
+  set (OPT_BIND_DIRECTIVE "-B ${INSTALL_BIND_PATH}:${INSTALL_BIND_PATH} ")
+  message("Adding singularity bind directive: '${OPT_BIND_DIRECTIVE}'")
+endif ()
+
+find_program(SINGULARITY singularity)
+
+foreach(APP ${APPS})
+  configure_file(launcher.in ${APP} @ONLY)
+  set(APPS_ABSOLUTE_PATH ${APPS_ABSOLUTE_PATH} "${CMAKE_CURRENT_BINARY_DIR}/${APP}")
+endforeach ()
+
+################################################################################
+## Install the launcher scripts to <prefix>/bin
+################################################################################
+install(PROGRAMS ${APPS_ABSOLUTE_PATH}
+  DESTINATION "${INSTALL_BIN_DIR}" COMPONENT bin)
diff --git a/scripts/launcher.in b/scripts/launcher.in
new file mode 100755
index 0000000..789b0dd
--- /dev/null
+++ b/scripts/launcher.in
@@ -0,0 +1,16 @@
+#!/bin/bash
+
+piped_args=
+if [ -p /dev/stdin ]; then
+  # If we want to read the input line by line
+  while IFS= read line; do
+    #echo "Line: ${line}"
+    if [ -z "$piped_args" ]; then
+      piped_args="${line}"
+    else 
+      piped_args="${piped_args}\n${line}"
+    fi
+  done
+fi
+
+echo -e ${piped_args} | @SINGULARITY@ exec @OPT_BIND_DIRECTIVE@@INSTALL_LIB_DIR@/@CONTAINER@ @APP@ $@
-- 
GitLab