Skip to content

Commit b8be512

Browse files
author
rpfische
committed
Nice build with CMake.
1 parent d760368 commit b8be512

17 files changed

+435
-95
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@
2626
*.exe
2727
*.out
2828
*.app
29+
30+
build
31+
*~

CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
cmake_minimum_required(VERSION 3.2)
2+
enable_language(Fortran)
3+
4+
list (APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
5+
include (setup_rpath)
6+
7+
# ------- Process Compile Options
8+
if (NOT DEFINED THREAD_SAFE)
9+
set(THREAD_SAFE NO)
10+
endif()
11+
12+
if(${THREAD_SAFE})
13+
# This will require boost::thread
14+
add_definitions(-DTHREAD_SAFE)
15+
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DTHREAD_SAFE")
16+
endif()
17+
18+
19+
20+
# -------- Locate External Libraries
21+
find_package(Python REQUIRED)
22+
if(${THREAD_SAFE})
23+
find_package(Boost COMPONENTS thread)
24+
endif()
25+
26+
add_subdirectory(slib)
27+
add_subdirectory(pyext)
28+
add_subdirectory(examples)

cmake/FindPython.cmake

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
# - Find python executable and libraries.
2+
#
3+
# PYTHON_EXECUTABLE - name of the python executable.
4+
# PYTHON_INCLUDES - where to find Python.h, etc.
5+
# PYTHON_FOUND - True if python is found
6+
#
7+
# http://stackoverflow.com/questions/13298504/using-cmake-with-setup-py
8+
9+
#if(PYTHON_EXECUTABLE AND PYTHON_INCLUDES AND PYTHON_LIBRARY )
10+
# set(PYTHON_FIND_QUIETLY TRUE)
11+
#endif()
12+
13+
message(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
14+
find_program(PYTHON_EXECUTABLE python DOC "python interpreter")
15+
16+
if(PYTHON_EXECUTABLE)
17+
execute_process( COMMAND ${PYTHON_EXECUTABLE} -c "import sysconfig; print sysconfig.get_path('include')"
18+
OUTPUT_VARIABLE PYTHON_INCLUDES
19+
RESULT_VARIABLE PYTHON_INCLUDES_NOT_FOUND
20+
OUTPUT_STRIP_TRAILING_WHITESPACE)
21+
execute_process( COMMAND ${PYTHON_EXECUTABLE} -c "import sysconfig; print sysconfig.get_config_var('LIBPL')"
22+
OUTPUT_VARIABLE PYTHON_LIBDIR
23+
RESULT_VARIABLE PYTHON_LIBDIR_NOT_FOUND
24+
OUTPUT_STRIP_TRAILING_WHITESPACE)
25+
execute_process( COMMAND ${PYTHON_EXECUTABLE} -c "import sysconfig; print sysconfig.get_python_version()"
26+
OUTPUT_VARIABLE PYTHON_VERSION
27+
RESULT_VARIABLE PYTHON_VERSION_NOT_FOUND
28+
OUTPUT_STRIP_TRAILING_WHITESPACE)
29+
30+
endif()
31+
32+
if(PYTHON_LIBDIR)
33+
find_library( PYTHON_LIBRARY "python${PYTHON_VERSION}" HINTS ${PYTHON_LIBDIR} NO_CMAKE_PATH NO_CMAKE_ENVIRONMENT_PATH NO_SYSTEM_ENVIRONMENT_PATH NO_DEFAULT_PATH)
34+
message("python${PYTHON_VERSION}")
35+
endif()
36+
37+
message("-- Found PYTHON_EXECUTABLE " ${PYTHON_EXECUTABLE})
38+
message("-- Found PYTHON_VERSION " ${PYTHON_VERSION})
39+
message("-- Found PYTHON_INCLUDES " ${PYTHON_INCLUDES})
40+
message("-- Found PYTHON_LIBDIR " ${PYTHON_LIBDIR})
41+
message("-- Found PYTHON_LIBRARY " ${PYTHON_LIBRARY})
42+
43+
44+
# if(PYTHON_EXECUTABLE)
45+
# if(NOT PYTHON_FIND_QUIETLY)
46+
# message( STATUS "Found Python executable: ${PYTHON_EXECUTABLE}")
47+
# endif()
48+
# else()
49+
# if(FIND_PYTHON_REQUIRED)
50+
# message( FATAL_ERROR "Python executable missing")
51+
# endif()
52+
# endif()
53+
#
54+
# if(PYTHON_INCLUDES)
55+
# if(NOT PYTHON_FIND_QUIETLY)
56+
# message( STATUS "Found Python includes: ${PYTHON_INCLUDES}")
57+
# endif()
58+
# else()
59+
# if(FIND_PYTHON_REQUIRED)
60+
# message( FATAL_ERROR "Python include directory missing")
61+
# endif()
62+
# endif()
63+
#
64+
# if(PYTHON_LIBRARY)
65+
# if(NOT PYTHON_FIND_QUIETLY)
66+
# message( STATUS "Found Python library: ${PYTHON_LIBRARY}")
67+
# endif()
68+
# else()
69+
# if(FIND_PYTHON_REQUIRED)
70+
# message( FATAL_ERROR "Python library missing")
71+
# endif()
72+
# endif()
73+
74+
MARK_AS_ADVANCED(PYTHON_EXECUTABLE PYTHON_INCLUDES PYTHON_LIBRARY)
75+
76+
# --------------------------------------------
77+
# Grab paths out of Python's sysconfig module
78+
# https://docs.python.org/2/library/sysconfig.html#module-sysconfig
79+
80+
set(GET_PATH_ARGS "")
81+
if(DEFINED PY_SYSCONFIG_SCHEME)
82+
set(GET_PATH_ARGS ${GET_PATH_ARGS} ", scheme='${PY_SYSCONFIG_SCHEME}'")
83+
endif()
84+
85+
set(GET_PATH_VARS "'__dummy__' : 17")
86+
if (DEFINED PY_SYSCONFIG_BASE)
87+
set(GET_PATH_VARS "${GET_PATH_VARS}, 'base' : '${PY_SYSCONFIG_BASE}'")
88+
endif()
89+
if (DEFINED PY_SYSCONFIG_USERBASE)
90+
set(GET_PATH_VARS "${GET_PATH_VARS}, 'userbase' : '${PY_SYSCONFIG_USERBASE}'")
91+
endif()
92+
set(GET_PATH_ARGS "${GET_PATH_ARGS}, vars={${GET_PATH_VARS}}")
93+
94+
95+
#set(GET_PATH_ARGS "${GET_PATH_ARGS}, expand=False")
96+
97+
message("-- Constructed GET_PATH_ARGS " ${GET_PATH_ARGS})
98+
99+
if(NOT DEFINED PY_SYSCONFIG_STDLIB)
100+
execute_process( COMMAND ${PYTHON_EXECUTABLE} -c "import sysconfig; print sysconfig.get_path('stdlib' ${GET_PATH_ARGS})"
101+
OUTPUT_VARIABLE PY_SYSCONFIG_STDLIB
102+
RESULT_VARIABLE PY_SYSCONFIG_STLIB_NOT_FOUND
103+
OUTPUT_STRIP_TRAILING_WHITESPACE)
104+
endif()
105+
message("-- Found PY_SYSCONFIG_STDLIB " ${PY_SYSCONFIG_STDLIB})
106+
107+
if(NOT DEFINED PY_SYSCONFIG_PLATSTDLIB)
108+
execute_process( COMMAND ${PYTHON_EXECUTABLE} -c "import sysconfig; print sysconfig.get_path('platstdlib' ${GET_PATH_ARGS})"
109+
OUTPUT_VARIABLE PY_SYSCONFIG_PLATSTDLIB
110+
RESULT_VARIABLE PY_SYSCONFIG_STLIB_NOT_FOUND
111+
OUTPUT_STRIP_TRAILING_WHITESPACE)
112+
endif()
113+
message("-- Found PY_SYSCONFIG_PLATSTDLIB " ${PY_SYSCONFIG_PLATSTDLIB})
114+
115+
if(NOT DEFINED PY_SYSCONFIG_PLATLIB)
116+
execute_process( COMMAND ${PYTHON_EXECUTABLE} -c "import sysconfig; print sysconfig.get_path('platlib' ${GET_PATH_ARGS})"
117+
OUTPUT_VARIABLE PY_SYSCONFIG_PLATLIB
118+
RESULT_VARIABLE PY_SYSCONFIG_STLIB_NOT_FOUND
119+
OUTPUT_STRIP_TRAILING_WHITESPACE)
120+
endif()
121+
message("-- Found PY_SYSCONFIG_PLATLIB " ${PY_SYSCONFIG_PLATLIB})
122+
123+
if(NOT DEFINED PY_SYSCONFIG_PURELIB)
124+
execute_process( COMMAND ${PYTHON_EXECUTABLE} -c "import sysconfig; print sysconfig.get_path('purelib' ${GET_PATH_ARGS})"
125+
OUTPUT_VARIABLE PY_SYSCONFIG_PURELIB
126+
RESULT_VARIABLE PY_SYSCONFIG_STLIB_NOT_FOUND
127+
OUTPUT_STRIP_TRAILING_WHITESPACE)
128+
endif()
129+
message("-- Found PY_SYSCONFIG_PURELIB " ${PY_SYSCONFIG_PURELIB})
130+
131+
if(NOT DEFINED PY_SYSCONFIG_INCLUDE)
132+
execute_process( COMMAND ${PYTHON_EXECUTABLE} -c "import sysconfig; print sysconfig.get_path('include' ${GET_PATH_ARGS})"
133+
OUTPUT_VARIABLE PY_SYSCONFIG_INCLUDE
134+
RESULT_VARIABLE PY_SYSCONFIG_STLIB_NOT_FOUND
135+
OUTPUT_STRIP_TRAILING_WHITESPACE)
136+
endif()
137+
message("-- Found PY_SYSCONFIG_INCLUDE " ${PY_SYSCONFIG_INCLUDE})
138+
139+
# Docs say this should work but it doesn't
140+
#execute_process( COMMAND ${PYTHON_EXECUTABLE} -c "import sysconfig; print sysconfig.get_path('platinclude' ${GET_PATH_ARGS})"
141+
# OUTPUT_VARIABLE PY_SYSCONFIG_PLATINCLUDE
142+
# RESULT_VARIABLE PY_SYSCONFIG_STLIB_NOT_FOUND
143+
# OUTPUT_STRIP_TRAILING_WHITESPACE)
144+
#endif()
145+
#message("-- Found PY_SYSCONFIG_PLATINCLUDE " ${PY_SYSCONFIG_PLATINCLUDE})
146+
147+
if(NOT DEFINED PY_SYSCONFIG_SCRIPTS)
148+
execute_process( COMMAND ${PYTHON_EXECUTABLE} -c "import sysconfig; print sysconfig.get_path('scripts' ${GET_PATH_ARGS})"
149+
OUTPUT_VARIABLE PY_SYSCONFIG_SCRIPTS
150+
RESULT_VARIABLE PY_SYSCONFIG_STLIB_NOT_FOUND
151+
OUTPUT_STRIP_TRAILING_WHITESPACE)
152+
endif()
153+
message("-- Found PY_SYSCONFIG_SCRIPTS " ${PY_SYSCONFIG_SCRIPTS})
154+
155+
if(NOT DEFINED PY_SYSCONFIG_DATA)
156+
execute_process( COMMAND ${PYTHON_EXECUTABLE} -c "import sysconfig; print sysconfig.get_path('data' ${GET_PATH_ARGS})"
157+
OUTPUT_VARIABLE PY_SYSCONFIG_DATA
158+
RESULT_VARIABLE PY_SYSCONFIG_STLIB_NOT_FOUND
159+
OUTPUT_STRIP_TRAILING_WHITESPACE)
160+
endif()
161+
message("-- Found PY_SYSCONFIG_DATA " ${PY_SYSCONFIG_DATA})
162+
163+
164+

cmake/setup_rpath.cmake

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# enable @rpath in the install name for any shared library being built
2+
# note: it is planned that a future version of CMake will enable this by default
3+
set(CMAKE_MACOSX_RPATH 1)
4+
5+
# --------------------------------------------------------
6+
# Always use full RPATH
7+
# http://www.cmake.org/Wiki/CMake_RPATH_handling
8+
# http://www.kitware.com/blog/home/post/510
9+
10+
# use, i.e. don't skip the full RPATH for the build tree
11+
SET(CMAKE_SKIP_BUILD_RPATH FALSE)
12+
13+
# when building, don't use the install RPATH already
14+
# (but later on when installing)
15+
SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
16+
17+
# add the automatically determined parts of the RPATH
18+
# which point to directories outside the build tree to the install RPATH
19+
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
20+
21+
# the RPATH to be used when installing, but only if it's not a system directory
22+
LIST(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir)
23+
IF("${isSystemDir}" STREQUAL "-1")
24+
SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
25+
ENDIF("${isSystemDir}" STREQUAL "-1")
26+
27+
message("-- CMAKE_INSTALL_RPATH " ${CMAKE_INSTALL_RPATH})
28+
# --------------------------------------------------------

configme/macports

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/sh
2+
3+
MACPORTS=$HOME/macports
4+
5+
#export BOOST_INCLUDEDIR=$BOOST_ROOT/include
6+
#export BOOST_LIBRARYDIR=$BOOST_ROOT/lib
7+
8+
# -DBOOST_ROOT=$HOME/opt/boost-1.55.0 \
9+
10+
11+
#PYTHON_ROOT=`python-config --prefix`
12+
13+
14+
#PYTHON_LIBRARY - path to the python library
15+
#PYTHON_INCLUDE_DIR
16+
17+
cmake \
18+
-DCMAKE_CXX_COMPILER=g++ \
19+
-DCMAKE_C_COMPILER=gcc \
20+
-DCMAKE_Fortran_COMPILER=gfortran \
21+
-DCMAKE_INSTALL_PREFIX:PATH=$HOME/opt/fexception \
22+
-DPY_SYSCONFIG_SCHEME=posix_user \
23+
-DPYTHON_EXECUTABLE=`which python` \
24+
-DTHREAD_SAFE=YES \
25+
$@
26+
27+
# -DPY_SYSCONFIG_USERBASE=/hhhhhhhhhhh \
28+
29+
# Or set custom Python install scheme...
30+
# -DPY_SYSCONFIG_STDLIB=/hhhhhhhhhhhhhhhhhhh/17 \
31+
32+
# -DWITH_GLINT2
33+
# -DPYTHON_INCLUDES=`python-config --includes` \
34+
# -DPYTHON_LIBS=`python-conifg --libs` \

examples/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
include_directories(
3+
${CMAKE_SOURCE_DIR}/slib
4+
)
5+
6+
add_executable (example_fexception_c example_fexception_c.cpp)
7+
target_link_libraries(example_fexception_c fexception)
8+
9+
add_executable (example_fexception_f example_fexception_f.F90)
10+
target_link_libraries(example_fexception_f fexception)

examples/example_fexception.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from fexception import *
2+
3+
4+
def sample_fn(val):
5+
if val == 17:
6+
fthrow('Seventeen detected', 17)
7+
else:
8+
print 'Running sample_fn {}'.format(val)
9+
10+
try:
11+
ftry(lambda: sample_fn(val+1))
12+
except Exception as e:
13+
frethrow()
14+
15+
16+
try:
17+
ftry(lambda: sample_fn(5))
18+
except Exception as e:
19+
print e
20+
21+
try:
22+
ftry(lambda: sample_fn(17))
23+
except Exception as e:
24+
print e
25+

examples/example_fexception_c.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <fexception_c.hpp>
2+
3+
struct Context {
4+
int val;
5+
};
6+
void sample_fn(void *vapi) throw(FException *)
7+
{
8+
Context *api = (Context *)vapi;
9+
if (api->val == 17) {
10+
fexception_throw((char *)"Seventeen detected", -1, 17);
11+
} else {
12+
printf("Running sample_fn %d\n", api->val);
13+
Context napi;
14+
napi.val = api->val + 1;
15+
if (fexception_try(sample_fn, &napi)) {
16+
fexception_rethrow();
17+
}
18+
}
19+
}
20+
21+
void myunexpected() {
22+
printf("unexpected() was called, continuing...\n");
23+
}
24+
25+
int main(int argc, char **argv)
26+
{
27+
Context api;
28+
29+
#if 0
30+
try{
31+
api.val = 5;
32+
sample_fn(&api);
33+
api.val = 17;
34+
sample_fn(&api);
35+
} catch(FException *e) {
36+
std::cout << "Caught C++ exception: " << e->msg << std::endl;
37+
}
38+
#endif
39+
40+
FException const *exp;
41+
api.val = 5;
42+
if ((exp = fexception_try(sample_fn, &api))) {
43+
printf("fexception_try threw 1: %d, %s\n", exp->code, exp->msg);
44+
}
45+
api.val = 17;
46+
if ((exp = fexception_try(sample_fn, &api))) {
47+
printf("fexception_try threw 2: %d %s\n", exp->code, exp->msg);
48+
}
49+
50+
#if 0
51+
try{
52+
api.val = 17;
53+
sample_fn(&api);
54+
} catch(FException *e) {
55+
std::cout << "Caught C++ exception: " << e->msg << std::endl;
56+
}
57+
#endif
58+
59+
60+
}

src/example_fexception_f.F90 renamed to examples/example_fexception_f.F90

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ subroutine main()
5353
print *, 'try threw:', exception%code
5454
end if
5555

56+
api%val = 17
57+
if (try(c_funloc(sample_fn), c_loc(api), exception)) then
58+
f_str = c_f_string(exception%c_msg)
59+
print *, 'try threw:', exception%code
60+
end if
61+
5662

5763
end subroutine main
5864

0 commit comments

Comments
 (0)