|
| 1 | +#/ |
| 2 | +# @license Apache-2.0 |
| 3 | +# |
| 4 | +# Copyright (c) 2018 The Stdlib Authors. |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +#/ |
| 18 | + |
| 19 | +# VARIABLES # |
| 20 | + |
| 21 | +ifndef VERBOSE |
| 22 | + QUIET := @ |
| 23 | +else |
| 24 | + QUIET := |
| 25 | +endif |
| 26 | + |
| 27 | +# Determine the OS ([1][1], [2][2]). |
| 28 | +# |
| 29 | +# [1]: https://en.wikipedia.org/wiki/Uname#Examples |
| 30 | +# [2]: http://stackoverflow.com/a/27776822/2225624 |
| 31 | +OS ?= $(shell uname) |
| 32 | +ifneq (, $(findstring MINGW,$(OS))) |
| 33 | + OS := WINNT |
| 34 | +else |
| 35 | +ifneq (, $(findstring MSYS,$(OS))) |
| 36 | + OS := WINNT |
| 37 | +else |
| 38 | +ifneq (, $(findstring CYGWIN,$(OS))) |
| 39 | + OS := WINNT |
| 40 | +else |
| 41 | +ifneq (, $(findstring Windows_NT,$(OS))) |
| 42 | + OS := WINNT |
| 43 | +endif |
| 44 | +endif |
| 45 | +endif |
| 46 | +endif |
| 47 | + |
| 48 | +# Determine the absolute path of the Makefile (see http://blog.jgc.org/2007/01/what-makefile-am-i-in.html): |
| 49 | +this_dir := $(dir $(CURDIR)/$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))) |
| 50 | + |
| 51 | +# Remove the trailing slash: |
| 52 | +this_dir := $(patsubst %/,%,$(this_dir)) |
| 53 | + |
| 54 | +# Define the command for Node.js: |
| 55 | +NODE ?= node |
| 56 | + |
| 57 | +# Define the Node.js directory path. |
| 58 | +# |
| 59 | +# ## Notes |
| 60 | +# |
| 61 | +# 1. The directory is expected to contain include header files. |
| 62 | +# 2. This only works on Mac and Linux. |
| 63 | +NODE_DIR ?= $(shell $(NODE) -e 'console.log( path.resolve( process.execPath, "..", ".." ) );') |
| 64 | + |
| 65 | +# Determine path to NAN header files: |
| 66 | +INCLUDE_NAN ?= $(shell node -e 'require( "nan" );') |
| 67 | + |
| 68 | +# Define the program used for compiling C source files: |
| 69 | +ifdef C_COMPILER |
| 70 | + CC := $(C_COMPILER) |
| 71 | +else |
| 72 | + CC := gcc |
| 73 | +endif |
| 74 | + |
| 75 | +# Define the command-line options when compiling C files: |
| 76 | +CFLAGS ?= \ |
| 77 | + -std=c99 \ |
| 78 | + -O3 \ |
| 79 | + -Wall \ |
| 80 | + -pedantic |
| 81 | + |
| 82 | +# Define the program used for compiling C++ source files: |
| 83 | +ifdef CXX_COMPILER |
| 84 | + CXX := $(CXX_COMPILER) |
| 85 | +else |
| 86 | + CXX := g++ |
| 87 | +endif |
| 88 | + |
| 89 | +# Define the command-line options when compiling C++ files: |
| 90 | +CXXFLAGS ?= \ |
| 91 | + -std=c++11 \ |
| 92 | + -O3 \ |
| 93 | + -Wall \ |
| 94 | + -pedantic |
| 95 | + |
| 96 | +# Determine whether to generate position independent code ([1][1], [2][2]). |
| 97 | +# |
| 98 | +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options |
| 99 | +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option |
| 100 | +ifeq ($(OS), WINNT) |
| 101 | + fPIC ?= |
| 102 | +else |
| 103 | + fPIC ?= -fPIC |
| 104 | +endif |
| 105 | + |
| 106 | +# Define the program for linking compiled files: |
| 107 | +ifdef LINKER |
| 108 | + LD := $(LINKER) |
| 109 | +else |
| 110 | + LD := g++ |
| 111 | +endif |
| 112 | + |
| 113 | +# Define the command-line options when linking compiled files: |
| 114 | +ifeq ($(OS), Darwin) |
| 115 | + LDFLAGS ?= \ |
| 116 | + -undefined dynamic_lookup \ |
| 117 | + -Wl,-no_pie \ |
| 118 | + -Wl,-search_paths_first |
| 119 | +else |
| 120 | + LDFLAGS ?= |
| 121 | +endif |
| 122 | + |
| 123 | +# List of external includes: |
| 124 | +INCLUDE ?= |
| 125 | + |
| 126 | +# List of external source files: |
| 127 | +SOURCE_FILES ?= |
| 128 | + |
| 129 | +# List of external object files: |
| 130 | +OBJECT_FILES ?= |
| 131 | + |
| 132 | +# List of libraries (e.g., `-lopenblas -lpthread`): |
| 133 | +LIBRARIES ?= |
| 134 | + |
| 135 | +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): |
| 136 | +LIBPATH ?= |
| 137 | + |
| 138 | +# List of C targets: |
| 139 | +c_objects := $(this_dir)/../../src/binary.o |
| 140 | + |
| 141 | +# List of C++ add-on targets: |
| 142 | +addon_objects := addon.o |
| 143 | + |
| 144 | + |
| 145 | +# RULES # |
| 146 | + |
| 147 | +#/ |
| 148 | +# Compiles source files. |
| 149 | +# |
| 150 | +# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) |
| 151 | +# @param {string} [CXX_COMPILER] - C++ compiler (e.g., `gcc`) |
| 152 | +# @param {string} [LINKER] - program for linking compiled files (e.g., `g++`) |
| 153 | +# @param {string} [CFLAGS] - C compiler options |
| 154 | +# @param {string} [CXXFLAGS] - C++ compiler options |
| 155 | +# @param {string} [LDFLAGS] - linker options |
| 156 | +# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) |
| 157 | +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) |
| 158 | +# @param {string} [SOURCE_FILES] - list of external source files |
| 159 | +# @param {string} [OBJECT_FILES] - list of external object files |
| 160 | +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) |
| 161 | +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`) |
| 162 | +# |
| 163 | +# @example |
| 164 | +# make |
| 165 | +# |
| 166 | +# @example |
| 167 | +# make all |
| 168 | +#/ |
| 169 | +all: $(c_objects) $(addon_objects) addon.node |
| 170 | + |
| 171 | +.PHONY: all |
| 172 | + |
| 173 | +#/ |
| 174 | +# Compiles C source files. |
| 175 | +# |
| 176 | +# @private |
| 177 | +# @param {string} CC - C compiler (e.g., `gcc`) |
| 178 | +# @param {string} CFLAGS - C compiler options |
| 179 | +# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) |
| 180 | +# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) |
| 181 | +#/ |
| 182 | +$(c_objects): %.o: %.c |
| 183 | + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -I ../../include -c -o $@ $< |
| 184 | + |
| 185 | +#/ |
| 186 | +# Compiles Node.js native add-ons. |
| 187 | +# |
| 188 | +# @private |
| 189 | +# @param {string} CXX - C++ compiler (e.g., `g++`) |
| 190 | +# @param {string} CXXFLAGS - C++ compiler options |
| 191 | +# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) |
| 192 | +# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop/`) |
| 193 | +# @param {string} INCLUDE_NAN - directory containing NAN header files |
| 194 | +# @param {string} NODE_DIR - Node.js directory path |
| 195 | +#/ |
| 196 | +$(addon_objects): %.o: %.cpp |
| 197 | + $(QUIET) $(CXX) \ |
| 198 | + $(CXXFLAGS) \ |
| 199 | + $(fPIC) \ |
| 200 | + -I "$(NODE_DIR)/include/node" \ |
| 201 | + -I "$(NODE_DIR)/src" \ |
| 202 | + -I "$(NODE_DIR)/deps/uv/include" \ |
| 203 | + -I "$(NODE_DIR)/deps/v8/include" \ |
| 204 | + -I "$(INCLUDE_NAN)" \ |
| 205 | + $(INCLUDE) \ |
| 206 | + -I ../../include \ |
| 207 | + -c \ |
| 208 | + -o $@ \ |
| 209 | + $< |
| 210 | + |
| 211 | +#/ |
| 212 | +# Creates a Node.js add-on shared object which can be linked to by other libraries and executables. |
| 213 | +# |
| 214 | +# @param {string} [LINKER] - program for linking compiled files (e.g., `g++`) |
| 215 | +# @param {string} [LDFLAGS] - linker flags |
| 216 | +# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) |
| 217 | +# @param {string} [OBJECT_FILES] - list of external object files to link |
| 218 | +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar`) |
| 219 | +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas`) |
| 220 | +# |
| 221 | +# @example |
| 222 | +# make addon.node |
| 223 | +#/ |
| 224 | +addon.node: $(addon_objects) |
| 225 | + $(QUIET) $(LD) -shared $(LDFLAGS) $(fPIC) -o $@ $(OBJECT_FILES) $(c_objects) $< $(LIBPATH) $(LIBRARIES) |
| 226 | + |
| 227 | +#/ |
| 228 | +# Runs compiled examples. |
| 229 | +# |
| 230 | +# @example |
| 231 | +# make run |
| 232 | +#/ |
| 233 | +run: addon.node |
| 234 | + $(QUIET) $(NODE) -e "var add = require( '$(this_dir)/addon.node' ).add; var x = new Float64Array( [0, 1, 2, 3, 4] ); var y = new Float64Array( [5, 6, 7, 8] ); var z = new Float64Array( x.length ); add( 5, x, 1, y, 1, z, 1 ); console.log( z );" |
| 235 | + |
| 236 | +.PHONY: run |
| 237 | + |
| 238 | +#/ |
| 239 | +# Removes generated Node.js add-on files. |
| 240 | +# |
| 241 | +# @example |
| 242 | +# make clean-addon |
| 243 | +#/ |
| 244 | +clean-addon: |
| 245 | + $(QUIET) -rm -f *.o *.node |
| 246 | + |
| 247 | +.PHONY: clean-addon |
| 248 | + |
| 249 | +#/ |
| 250 | +# Removes generated files. |
| 251 | +# |
| 252 | +# @example |
| 253 | +# make clean |
| 254 | +#/ |
| 255 | +clean: clean-addon |
| 256 | + |
| 257 | +.PHONY: clean |
0 commit comments