#!/bin/bash
show_help () {
echo "usage: llvm-config <OPTION>... [<COMPONENT>...]

Get various configuration information needed to compile programs which use
LLVM.  Typically called from 'configure' scripts.  Examples:
  llvm-config --cxxflags
  llvm-config --ldflags
  llvm-config --libs engine bcreader scalaropts

Options:
  --version         Print LLVM version.
  --prefix          Print the installation prefix.
  --src-root        Print the source root LLVM was built from.
  --obj-root        Print the object root used to build LLVM.
  --bindir          Directory containing LLVM executables.
  --includedir      Directory containing LLVM headers.
  --libdir          Directory containing LLVM libraries.
  --cppflags        C preprocessor flags for files that include LLVM headers.
  --cflags          C compiler flags for files that include LLVM headers.
  --cxxflags        C++ compiler flags for files that include LLVM headers.
  --ldflags         Print Linker flags.
  --system-libs     System Libraries needed to link against LLVM components.
  --libs            Libraries needed to link against LLVM components.
  --libnames        Bare library names for in-tree builds.
  --libfiles        Fully qualified library filenames for makefile depends.
  --components      List of all possible components.
  --targets-built   List of all targets currently built.
  --host-target     Target triple used to configure LLVM.
  --build-mode      Print build mode of LLVM tree (e.g. Debug or Release).
  --assertion-mode  Print assertion mode of LLVM tree (ON or OFF).
  --build-system    Print the build system used to build LLVM (always cmake).
  --has-rtti        Print whether or not LLVM was built with rtti (YES or NO).
  --has-global-isel Print whether or not LLVM was built with global-isel support (YES or NO).
  --shared-mode     Print how the provided components can be collectively linked (\`shared\` or \`static\`).
  --link-shared     Link the components as shared libraries.
  --link-static     Link the component libraries statically.
Typical components:
  all               All LLVM libraries (default).
  engine            Either a native JIT or a bitcode interpreter."
}

version=@LLVM_VERSION@
prefix=@LLVM_INSTALL_DIR@
has_rtti=NO
CPPFLAGS="-I${prefix}/include -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS"
CFLAGS="${CPPFLAGS} ${CFLAGS}"
CXXFLAGS="${CFLAGS} -std=c++14 -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables"
if [ "$has_rtti" != "YES" ]; then CXXFLAGS="$CXXFLAGS -fno-rtti"; fi
LDFLAGS="-L${prefix}/lib"
LIBFILE="${prefix}/lib/libLLVM-$version.so"

components="aarch64 aarch64asmparser aarch64codegen aarch64desc aarch64disassembler aarch64info aarch64utils \
aggressiveinstcombine all all-targets analysis arm armasmparser armcodegen armdesc armdisassembler arminfo armutils \
asmparser asmprinter binaryformat bitreader bitstreamreader bitwriter cfguard codegen core coroutines coverage \
debuginfocodeview debuginfodwarf debuginfogsym debuginfomsf debuginfopdb demangle dlltooldriver dwarflinker dwp \
engine executionengine extensions filecheck frontendopenacc frontendopenmp fuzzmutate globalisel instcombine \
instrumentation interfacestub interpreter ipo irreader jitlink libdriver lineeditor linker lto mc mca mcdisassembler \
mcjit mcparser mirparser native nativecodegen objcarcopts object objectyaml option orcjit orcshared orctargetprocess \
passes profiledata remarks runtimedyld scalaropts selectiondag support symbolize tablegen target textapi \
transformutils vectorize webassembly webassemblyasmparser webassemblycodegen webassemblydesc webassemblydisassembler \
webassemblyinfo webassemblyutils windowsmanifest x86 x86asmparser x86codegen x86desc x86disassembler x86info \
x86targetmca xray"
static_libs="-lLLVMWindowsManifest -lLLVMXRay -lLLVMLibDriver -lLLVMDlltoolDriver -lLLVMCoverage -lLLVMLineEditor \
-lLLVMX86TargetMCA -lLLVMX86Disassembler -lLLVMX86AsmParser -lLLVMX86CodeGen -lLLVMX86Desc -lLLVMX86Info -lLLVMWebAssemblyDisassembler \
-lLLVMWebAssemblyAsmParser -lLLVMWebAssemblyCodeGen -lLLVMWebAssemblyDesc -lLLVMWebAssemblyUtils -lLLVMWebAssemblyInfo -lLLVMARMDisassembler \
-lLLVMARMAsmParser -lLLVMARMCodeGen -lLLVMARMDesc -lLLVMARMUtils -lLLVMARMInfo -lLLVMAArch64Disassembler \
-lLLVMAArch64AsmParser -lLLVMAArch64CodeGen -lLLVMAArch64Desc -lLLVMAArch64Utils -lLLVMAArch64Info -lLLVMOrcJIT \
-lLLVMMCJIT -lLLVMJITLink -lLLVMInterpreter -lLLVMExecutionEngine -lLLVMRuntimeDyld -lLLVMOrcTargetProcess -lLLVMOrcShared \
-lLLVMDWP -lLLVMSymbolize -lLLVMDebugInfoPDB -lLLVMDebugInfoGSYM -lLLVMOption -lLLVMObjectYAML -lLLVMMCA \
-lLLVMMCDisassembler -lLLVMLTO -lLLVMPasses -lLLVMCFGuard -lLLVMCoroutines -lLLVMObjCARCOpts -lLLVMipo \
-lLLVMVectorize -lLLVMLinker -lLLVMInstrumentation -lLLVMFrontendOpenMP -lLLVMFrontendOpenACC -lLLVMExtensions \
-lLLVMDWARFLinker -lLLVMGlobalISel -lLLVMMIRParser -lLLVMAsmPrinter -lLLVMDebugInfoMSF -lLLVMSelectionDAG \
-lLLVMCodeGen -lLLVMIRReader -lLLVMAsmParser -lLLVMInterfaceStub -lLLVMFileCheck -lLLVMFuzzMutate -lLLVMTarget \
-lLLVMScalarOpts -lLLVMInstCombine -lLLVMAggressiveInstCombine -lLLVMTransformUtils -lLLVMBitWriter -lLLVMAnalysis \
-lLLVMProfileData -lLLVMDebugInfoDWARF -lLLVMObject -lLLVMTextAPI -lLLVMMCParser -lLLVMMC -lLLVMDebugInfoCodeView \
-lLLVMBitReader -lLLVMCore -lLLVMRemarks -lLLVMBitstreamReader -lLLVMBinaryFormat -lLLVMTableGen -lLLVMSupport \
-lLLVMDemangle"
shared_libs="-lLLVM-$version"
libs=$static_libs
handle_args () {
	if [ "${1:0:2}" != "--" ]; then return 0; fi
	case "${1:2}" in
		link-shared) libs=$shared_libs ;;
		link-static) libs=$static_libs ;;
		version) echo "$version";;
		prefix) echo "$prefix";;
		src-root) echo "@TERMUX_PKG_SRCDIR@";;
		obj-root) echo "$prefix";;
		bindir) echo "$prefix/bin";;
		includedir) echo "$prefix/include";;
		libdir) echo "$prefix/lib";;
		cppflags) echo "$CPPFLAGS";;
		cflags) echo "$CFLAGS";;
		cxxflags) echo "$CXXFLAGS";;
		ldflags) echo "$LDFLAGS";;
		system-libs) echo "-lc -ldl -lz -lm";;
		libs) echo "$libs";;
		libnames) echo "libLLVM-$version.so";;
		libfiles) echo "$LIBFILE";;
		components) echo "$components";;
		targets-built) echo "@LLVM_TARGETS@";;
		host-target) echo "@LLVM_DEFAULT_TARGET_TRIPLE@";;
		build-mode) echo "Release";;
		assertion-mode) echo "OFF";;
		build-system) echo "cmake";;
		has-rtti) echo "$has_rtti";;
		has-global-isel) echo "ON";;
		shared-mode) echo "static";;
		cmakedir) echo "$prefix/lib/cmake/llvm";;
		*) echo "Unsupported llvm-config switch: ${1}" >&2; exit 1;;
	esac
}

if [ "$#" -eq 0 ]; then
	show_help >&2
else
	for arg in $@; do handle_args $arg; done
fi
