CXX=em++

CUSTOM_CXXFLAGS = -std=c++11 -O3 -flto -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter -Wno-unused-but-set-variable -Wno-unused-function

# list of CXXFLAGS extracted from autotools output
CXXFLAGS=$(CUSTOM_CXXFLAGS) $(shell echo '-DPACKAGE_NAME=\"BEM\" -DPACKAGE_TARNAME=\"bem\" -DPACKAGE_VERSION=\"1.0\" -DPACKAGE_STRING=\"BEM\ 1.0\" -DPACKAGE_BUGREPORT=\"zahn.sharon@mayo.edu\" -DPACKAGE_URL=\"\" -DPACKAGE=\"bem\" -DVERSION=\"1.0\" -DHAVE_STDIO_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_STRINGS_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_UNISTD_H=1 -DSTDC_HEADERS=1 -DHAVE_FCNTL_H=1 -DHAVE_FLOAT_H=1 -DHAVE_LIMITS_H=1 -DHAVE_MALLOC_H=1 -DHAVE_STDDEF_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_UNISTD_H=1 -DHAVE_PWD_H=1 -DF77_FUNC\(name,NAME\)=name\ ##\ _ -DF77_FUNC_\(name,NAME\)=name\ ##\ _ -DGETGROUPS_T=gid_t -DHAVE_GETGROUPS=1 -DHAVE_MALLOC=1 -DLSTAT_FOLLOWS_SLASHED_SYMLINK=1 -DHAVE_STRFTIME=1 -DHAVE_VPRINTF=1 -DHAVE_GETCWD=1 -DHAVE_GETLOGIN=1 -DHAVE_RMDIR=1 -DHAVE_STRCHR=1 -DHAVE_STRDUP=1 -DHAVE_STRRCHR=1 -DHAVE_STRSTR=1 -I.    -DFORTRAN_UNDERBARS')

SRC_DIR ?= ../../src

# list of C++ files
.cpp_src_noprefix = \
	assemble.cpp assemble_free_space.cpp dim2.cpp electro_read_icon_section.cpp \
	error_location.cpp free2.cpp in_string.cpp math_library.cpp nmmtl_add_to_sorted_list.cpp \
	nmmtl_angle_of_intersection.cpp nmmtl_build_gnd_die_list.cpp nmmtl_charge.cpp \
	nmmtl_circle_segments.cpp nmmtl_charimp_propvel_calculate.cpp nmmtl_combine_die.cpp \
	nmmtl_containment.cpp nmmtl_det_arc_intersections.cpp nmmtl_det_intersections.cpp \
	nmmtl_dump.cpp nmmtl_dump_geometry.cpp nmmtl_eval_circles.cpp nmmtl_eval_conductors.cpp \
	nmmtl_eval_polygons.cpp nmmtl_eval_rectangles.cpp nmmtl_fill_die_gaps.cpp \
	nmmtl_find_ground_planes.cpp nmmtl_find_nu.cpp nmmtl_form_die_subseg.cpp \
	nmmtl_gen_netlist_from_icon.cpp nmmtl_genel.cpp nmmtl_genel_ccs.cpp \
	nmmtl_genel_cls.cpp nmmtl_genel_die.cpp nmmtl_genel_gnd.cpp nmmtl_intersections.cpp \
	nmmtl_interval.cpp nmmtl_jacobian.cpp nmmtl_load.cpp nmmtl_merge_die_subseg.cpp \
	nmmtl_new_die_seg.cpp nmmtl_nl_expand.cpp nmmtl_orphans.cpp nmmtl_output_headers.cpp \
	nmmtl_output_matrices.cpp nmmtl_overlap_parallel_seg.cpp nmmtl_dc_resistance.cpp \
	nmmtl_qsp_calculate.cpp nmmtl_qsp_kernel.cpp nmmtl_retrieve.cpp nmmtl_set_offset.cpp \
	nmmtl_shape.cpp nmmtl_spout_off.cpp nmmtl_sort_gnd_die_list.cpp nmmtl_unload.cpp \
	nmmtl_write_plot_data.cpp nmmtl_xtk_calculate.cpp output_charimp_propvel.cpp \
	output_crosstalk.cpp plotFileInitialization.cpp remove_all_spaces.cpp units.cpp \
	nmmtl.cpp nmmtl_parse_xsctn.cpp

# Add $SRC_DIR prefix to all files to allow out-of-tree build
.cpp_src = $(addprefix $(SRC_DIR), $(.cpp_src_noprefix))

# Replace .cpp to .o to get output object code file
.cpp_obj = $(.cpp_src_noprefix:.cpp=.o)

bem.js: $(.cpp_obj)
	$(CXX) $(CXXFLAGS) $(.cpp_obj) nswc.a -o bem.js -sEXPORTED_RUNTIME_METHODS=callMain,FS

%.o: $(SRC_DIR)/%.cpp
	$(CXX) -c $(CXXFLAGS) $(SRC_DIR)/$*.cpp -o $*.o

clean:
	rm -rf *.ll *.o *.wasm *.a *.js *.data
