RN rnmake  3.0.0
Rules.swig.mk
Go to the documentation of this file.
1 ################################################################################
2 #
3 # Rules.swig.mk
4 #
5 ifdef RNMAKE_DOXY
6 /*!
7 \file
8 
9 \brief Special rules file for swigging C into python.
10 
11 Include this file in each appropriate local make file (usually near the bottom).
12 
13 \par Key RNMAKE Variables:
14  \li SWIG_FILES - list of swig *.i interface files.
15  \li SWIG_EXTMOD_DIR - output *.py and shared libraries directory.
16  \li SWIG_EXTMOD_LIBS - list of -l<lib> libraries to link in.
17 
18 \pkgsynopsis
19 RN Make System
20 
21 \pkgfile{Rules.swig.mk}
22 
23 \pkgauthor{Robin Knight,robin.knight@roadnarrows.com}
24 
25 \pkgcopyright{2010-2018,RoadNarrows LLC,http://www.roadnarrows.com}
26 
27 \license{MIT}
28 
29 \EulaBegin
30 \EulaEnd
31 
32 \cond RNMAKE_DOXY
33  */
34 endif
35 #
36 ################################################################################
37 
38 _RULES_SWIG_MK = 1
39 
40 ifeq ($(strip $(RNMAKE_SWIG_ENABLED)),y)
41 
42 # Simplified Wrapper and Interface Generator command
43 SWIG = /usr/bin/swig
44 
45 SWIG_BASES = $(basename $(SWIG_FILES))
46 SWIG_FILES_C = $(addsuffix _wrap.c,$(SWIG_BASES))
47 SWIG_FILES_O = $(addprefix $(OBJDIR)/,$(subst .c,.o,$(SWIG_FILES_C)))
48 SWIG_FILES_PY = $(addprefix $(SWIG_EXTMOD_DIR)/,$(addsuffix .py,$(SWIG_BASES)))
49 SWIG_EXTMODS = $(addprefix $(SWIG_EXTMOD_DIR)/_,\
50  $(addsuffix $(SHLIB_SUFFIX),$(SWIG_BASES)))
51 
52 # Get python version stripped of revision number.
53 # Examples outputs from python --version:
54 # Python 2.7.6
55 # Python 2.7.11+
56 PYTHON_VER = $(shell python --version 2>&1 | sed -e 's/Python //' -e's/\.[0-9]*[\+]*$$//' )
57 
58 ifeq "$(RNMAKE_ARCH)" "cygwin"
59 SWIG_PYLIB = -lpython$(PYTHON_VER).dll
60 else
61 SWIG_PYLIB = -lpython$(PYTHON_VER)
62 endif
63 
64 ifndef "$(SWIG_INCLUDES)"
65  SWIG_INCLUDES = -I/usr/include/python$(PYTHON_VER)
66 endif
67 
68 
69 SWIG_LIBS = $(SWIG_EXTMOD_LIBS) $(SWIG_PYLIB)
70 
71 SWIG_DONE = $(RNMAKE_ARCH).done
72 
73 # C and link loader flags
74 CFLAGS := $(EXTRA_CFLAGS) $(RNMAKE_PKG_CFLAGS) $(SWIG_CFLAGS)
75 LDFLAGS := $(EXTRA_LDFLAGS) $(RNMAKE_PKG_LDFLAGS) $(SWIG_LDFLAGS)
76 
77 define cond-clean
78 endef
79 
80 # Target: swig-all (default)
81 .PHONY: swig-all
82 swig-all: echo-swig-all swig-pre swig-mods
83  @$(TOUCH) $(SWIG_DONE)
84 
85 .PHONY: echo-swig-all
86 echo-swig-all:
87  $(call printEchoTgtGoalDesc,Creating python wrappers from C/C++ source)
88 
89 # Target: swig-pre
90 # If targets were made for another architecture, then clean up so that targets
91 # will be remade for the target architecture. The file done.<arch> determines
92 # the last made target architecture.
93 #
94 .PHONY: swig-pre
95 swig-pre:
96  @if [ ! -f $(SWIG_DONE) ]; \
97  then \
98  if [ -f *.done ]; \
99  then \
100  printf "Cleaning previous target: $(basename $(wildcard *.done)).\n"; \
101  $(RM) *.done; \
102  fi; \
103  $(RM) $(OBJDIR); \
104  $(RM) $(SWIG_FILES_C); \
105  $(RM) $(SWIG_FILES_PY); \
106  $(RM) $(SWIG_EXTMODS); \
107  fi
108 
109 # Target: swig-mods
110 # Make all swig modules
111 swig-mods: $(SWIG_EXTMODS)
112 
113 # Target: swig-clean
114 swig-clean:
115  $(RM) $(SWIG_FILES_C)
116  $(RM) $(SWIG_FILES_PY)
117  $(RM) $(SWIG_EXTMODS)
118  $(RM) *.done
119 
120 # Done
121 # Make determines dependency sequence only for the first file (*.i) and the end
122 # file. Itermediates are not factored in. But rnmake supports cross-compiling,
123 # so the <name>.<arch>.done file is used as an artificial end file to force
124 # compliling.
125 #
126 %.$(RNMAKE_ARCH).done: $(SWIG_EXTMOD_DIR)/_%$(SHLIB_SUFFIX)
127  $(RM) *.done
128  $(TOUCH) $(@)
129 
130 # Swig Architecture-Specific Shared Library Rule:
131 $(SWIG_EXTMOD_DIR)/_%$(SHLIB_SUFFIX) : $(OBJDIR)/%_wrap.o
132  @printf "\n"
133  @printf "$(color_tgt_file) $(<)$(color_end)\n"
134  $(SHLIB_LD) $(LDFLAGS) $(LD_LIBPATHS) $(<) $(SWIG_LIBS) -o $(@)
135 
136 # Swig C Rule: <name>.c -> $(OBJDIR)/<name>.o
137 $(OBJDIR)/%.o : %.c
138  $(mkobjdir)
139  @printf "\n"
140  @printf "$(color_tgt_file) $(<)$(color_end)\n"
141  $(CC) $(CFLAGS) $(CPPFLAGS) $(INCLUDES) $(SWIG_INCLUDES) -o $(@) -c $(<)
142 
143 # Swig I Rule: <name>.i -> <name>_wrap.c, <outdir>/<name>.py
144 %_wrap.c : %.i
145  $(mkobjdir)
146  @printf "\n"
147  @printf "$(color_tgt_file) $(<)$(color_end)\n"
148  $(SWIG) -python $(INCLUDES) $(SWIG_INCLUDES) -v -outdir $(SWIG_EXTMOD_DIR) \
149  -o $(@) $(<)
150 
151 # don't autodelete intermediate files
152 .SECONDARY: $(SWIG_FILES_C) $(SWIG_FILES_O) $(SWIG_EXTMODS)
153 
154 else
155 
156 swig-all swig-clean:
157  @printf "swig not enabled - ignoring\n"
158 
159 endif
160 
161 ifdef RNMAKE_DOXY
162 /*! \endcond RNMAKE_DOXY */
163 endif