
# Makefile for Botan %{version} generated using
# '%{command_line}'

# Paths to relevant programs
CXX            = %{cxx}
LINKER         = %{linker}
AR             = %{ar_command}
AR_OPTIONS     = %{ar_options}
PYTHON_EXE     = %{python_exe}

# Compiler Flags
ABI_FLAGS      = %{cc_sysroot} %{cxx_abi_flags}
LANG_FLAGS     = %{cc_lang_flags} %{os_feature_macros}
LANG_EXE_FLAGS = %{cc_lang_binary_linker_flags}
CXXFLAGS       = %{cc_compile_flags} -DBOTAN_IS_BEING_BUILT
WARN_FLAGS     = %{cc_warning_flags}
LIB_FLAGS      = %{lib_flags}
LDFLAGS        = %{ldflags}

EXE_LINK_CMD   = %{exe_link_cmd}

LIB_LINKS_TO        = %{external_link_cmd} %{link_to}
BUILD_DIR_LINK_PATH = %{build_dir_link_path}
EXE_LINKS_TO        = %{link_to_botan} $(LIB_LINKS_TO) %{extra_libs}

# Note: Remember to adapt the generation of compile_commands.json, when changing
#       any of those variables or their associations to compilation units.
#       See compile_commands.json.in and configure.py (generate_build_info)
BUILD_FLAGS    = $(ABI_FLAGS) $(LANG_FLAGS) $(CXXFLAGS) $(WARN_FLAGS)

SCRIPTS_DIR    = %{scripts_dir}
INSTALLED_LIB_DIR = %{libdir}

# The primary target
all: %{all_targets}

# Executable targets
CLI           = %{cli_exe}
TEST          = %{test_exe}
LIBRARIES     = %{library_targets}

cli: $(CLI)
tests: $(TEST)
libs: $(LIBRARIES)
docs: %{doc_stamp_file}

# Misc targets

%{if make_supports_phony}
.PHONY: all cli libs tests check docs clean distclean install fmt tidy
%{endif}

%{doc_stamp_file}: %{doc_dir}/*.rst %{doc_dir}/api_ref/*.rst %{doc_dir}/dev_ref/*.rst
	"$(PYTHON_EXE)" "$(SCRIPTS_DIR)/build_docs.py" --build-dir="%{build_dir}"

clean:
	"$(PYTHON_EXE)" "$(SCRIPTS_DIR)/cleanup.py" --build-dir="%{build_dir}"

distclean:
	"$(PYTHON_EXE)" "$(SCRIPTS_DIR)/cleanup.py" --build-dir="%{build_dir}" --distclean

install: %{install_targets}
	"$(PYTHON_EXE)" "$(SCRIPTS_DIR)/install.py" --build-dir="%{build_dir}"

check: tests
	"$(PYTHON_EXE)" "$(SCRIPTS_DIR)/check.py" --build-dir="%{build_dir}"

fmt:
	"$(PYTHON_EXE)" "$(SCRIPTS_DIR)/dev_tools/run_clang_format.py"

tidy:
	"$(PYTHON_EXE)" "$(SCRIPTS_DIR)/dev_tools/run_clang_tidy.py" --only-changed-files

# Object Files
LIBOBJS = %{join lib_objs}

CLIOBJS = %{join cli_objs}

TESTOBJS = %{join test_objs}

# Executable targets

$(CLI): $(LIBRARIES) $(CLIOBJS)
	$(EXE_LINK_CMD) $(ABI_FLAGS) $(CLIOBJS) $(BUILD_DIR_LINK_PATH) $(LANG_EXE_FLAGS) $(LDFLAGS) $(EXE_LINKS_TO) %{output_to_exe}$@

$(TEST): $(LIBRARIES) $(TESTOBJS)
	$(EXE_LINK_CMD) $(ABI_FLAGS) $(TESTOBJS) $(BUILD_DIR_LINK_PATH) $(LANG_EXE_FLAGS) $(LDFLAGS) %{test_exe_extra_ldflags} $(EXE_LINKS_TO) %{output_to_exe}$@

%{if build_fuzzers}

FUZZERS = %{fuzzer_bin}

fuzzers: $(LIBRARIES) $(FUZZERS)

fuzzer_corpus:
	git clone --depth=1 https://github.com/randombit/crypto-corpus.git fuzzer_corpus

fuzzer_corpus_zip: fuzzer_corpus
	%{base_dir}/src/scripts/create_corpus_zip.py fuzzer_corpus %{fuzzobj_dir}

%{endif}

%{if build_examples}

EXAMPLES = %{example_bin}

examples: $(LIBRARIES) $(EXAMPLES)

%{endif}

%{if build_bogo_shim}

bogo_shim: %{out_dir}/botan_bogo_shim

# BoGo shim
%{out_dir}/botan_bogo_shim: %{bogo_shim_src} $(LIBRARIES)
	$(CXX) $(BUILD_FLAGS) %{include_paths} %{bogo_shim_src} $(BUILD_DIR_LINK_PATH) $(LDFLAGS) $(EXE_LINKS_TO) %{output_to_exe}$@

%{endif}

# Library targets

%{if build_static_lib}

%{out_dir}/%{static_lib_name}: $(LIBOBJS)
	$(AR) $(AR_OPTIONS) %{ar_output_to}$@ $(LIBOBJS)

%{endif}

%{if build_shared_lib}

%{out_dir}/%{shared_lib_name}: $(LIBOBJS)
	%{lib_link_cmd} $(ABI_FLAGS) $(LDFLAGS) $(LIBOBJS) $(LIB_LINKS_TO) %{output_to_exe}$@
%{endif}
%{if symlink_shared_lib}
	cd %{out_dir} && ln -fs %{shared_lib_name} %{soname_base}
	cd %{out_dir} && ln -fs %{shared_lib_name} %{soname_patch}
%{endif}

# Build Commands

%{for lib_build_info}
%{obj}: %{src}
	$(CXX) $(LIB_FLAGS) $(BUILD_FLAGS) %{isa_flags} %{include_paths} %{dash_c} %{src} %{dash_o}$@
%{endfor}

%{for cli_build_info}
%{obj}: %{src}
	$(CXX) $(BUILD_FLAGS) %{isa_flags} %{include_paths} %{dash_c} %{src} %{dash_o}$@
%{endfor}

%{for test_build_info}
%{obj}: %{src}
	$(CXX) $(BUILD_FLAGS) %{isa_flags} %{include_paths} %{dash_c} %{src} %{dash_o}$@
%{endfor}

%{for fuzzer_build_info}
%{obj}: %{src}
	$(CXX) $(BUILD_FLAGS) %{isa_flags} %{include_paths} %{dash_c} %{src} %{dash_o}$@

%{exe}: %{obj} $(LIBRARIES)
	$(EXE_LINK_CMD) $(ABI_FLAGS) %{obj} $(BUILD_DIR_LINK_PATH) $(LANG_EXE_FLAGS) $(LDFLAGS) $(EXE_LINKS_TO) %{fuzzer_lib} %{output_to_exe}$@
%{endfor}

%{for examples_build_info}
%{obj}: %{src}
	$(CXX) $(BUILD_FLAGS) %{isa_flags} %{include_paths} %{dash_c} %{src} %{dash_o}$@

%{exe}: %{obj} $(LIBRARIES)
	$(EXE_LINK_CMD) $(ABI_FLAGS) %{obj} $(BUILD_DIR_LINK_PATH) $(LANG_EXE_FLAGS) $(LDFLAGS) $(EXE_LINKS_TO) %{fuzzer_lib} %{output_to_exe}$@
%{endfor}
