// vim:ft=javascript
// "Master" config file; think of it as a configure.ac
// equivalent.

ARG_WITH("verbosity", "Output verbosity, 0-2.", "1");
setup_verbosity();

ARG_WITH("toolset", "Toolset to use for the compilation, give: vs, clang, icc. " +
		"The only recommended and supported toolset for production use " +
		"is Visual Studio. Use others at your own risk.", "vs");
toolset_option_handle();

ARG_WITH('cygwin', 'Path to cygwin utilities on your system', '\\cygwin');

toolset_setup_compiler();

// do we use x64 or 80x86 version of compiler?
X64 = toolset_is_64();
toolset_setup_arch();

toolset_setup_linker();
toolset_setup_project_tools();

// stick objects somewhere outside of the source tree
ARG_ENABLE('object-out-dir', 'Alternate location for binary objects during build', '');
object_out_dir_option_handle();

ARG_ENABLE('debug', 'Compile with debugging symbols', "no");
ARG_ENABLE('debug-pack', 'Release binaries with external debug symbols (--enable-debug must not be specified)', 'no');
if (PHP_DEBUG == "yes" && PHP_DEBUG_PACK == "yes") {
	ERROR("Use of both --enable-debug and --enable-debug-pack not allowed.");
}

if (PHP_DEBUG == "yes") {
	ADD_FLAG("CFLAGS"," /Wall ");
	ADD_FLAG("LDFLAGS", " /verbose ");
}

ARG_ENABLE('pgi', 'Generate PGO instrumented binaries', 'no');
ARG_WITH('pgo', 'Compile optimized binaries using training data from folder', 'no');
if (PHP_PGI == "yes" || PHP_PGO != "no") {
	PGOMGR = PATH_PROG('pgomgr', WshShell.Environment("Process").Item("PATH"));
	if (!PGOMGR) {
		ERROR("--enable-pgi and --with-pgo options can only be used if PGO capable compiler is present.");
	}
	if (PHP_PGI == "yes" && PHP_PGO != "no") {
		ERROR("Use of both --enable-pgi and --with-pgo not allowed.");
	}
}

ARG_ENABLE('zts', 'Thread safety', 'yes');
// Configures the hard-coded installation dir
ARG_WITH('prefix', 'where PHP will be installed', '');
if (PHP_PREFIX == '') {
	PHP_PREFIX = "C:\\php";
	if (PHP_DEBUG == "yes")
		PHP_PREFIX += "\\debug";
}
DEFINE('PHP_PREFIX', PHP_PREFIX);

DEFINE("BASE_INCLUDES", "/I . /I main /I Zend /I TSRM /I ext ");

toolset_setup_common_cflags();

if (VS_TOOLSET) {
	ARG_WITH('mp', 'Tell Visual Studio use up to [n,auto,disable] processes for compilation', 'auto');
	var PHP_MP_DISABLED = true;

	if (PHP_MP != 'disable') {
		if(PHP_DEBUG == 'yes') {
			STDOUT.WriteLine('WARNING: Debug builds cannot be built using multi processing');
		} else {
			// no from disable-all
			if(PHP_MP == 'auto' || PHP_MP == 'no') {
				ADD_FLAG('CFLAGS', ' /MP ');
				PHP_MP_DISABLED = false;
			} else {
				if(parseInt(PHP_MP) != 0) {
					ADD_FLAG('CFLAGS', ' /MP'+ PHP_MP +' ');
					PHP_MP_DISABLED = false;
				} else {
					STDOUT.WriteLine('WARNING: Invalid argument for MP: ' + PHP_MP);
				}
			}
		}
	}

	if (!PHP_MP_DISABLED) {
		STDOUT.WriteLine('Enabling multi process build');
	}
}

// General link flags
toolset_setup_common_ldlags();

// General libs
toolset_setup_common_libs();

// Set some debug/release specific options
toolset_setup_build_mode();

setup_zts_stuff();

// CFLAGS, LDFLAGS and BUILD_DIR are defined
// Add compiler and link flags if PGO options are selected
if (PHP_DEBUG != "yes" && PHP_PGI == "yes") {
	ADD_FLAG("STATIC_EXT_CFLAGS", "/GL /O2");
	DEFINE("PGOPGD_DIR", "$(BUILD_DIR)");
}
else if (PHP_DEBUG != "yes" && PHP_PGO != "no") {
	ADD_FLAG("STATIC_EXT_CFLAGS", "/GL /O2");
	DEFINE("PGOPGD_DIR", ((PHP_PGO.length == 0 || PHP_PGO == "yes") ? "$(BUILD_DIR)" : PHP_PGO));
}

// Find the php_build dir - it contains headers and libraries
// that we need
ARG_WITH('php-build', 'Path to where you extracted the development libraries (http://wiki.php.net/internals/windows/libs). Assumes that it is a sibling of this source dir (..\\deps) if not specified', 'no');
php_build_option_handle();

ARG_WITH('extra-includes', 'Extra include path to use when building everything', '');
ARG_WITH('extra-libs', 'Extra library path to use when linking everything', '');

var php_usual_include_suspects = PHP_PHP_BUILD+"\\include";
var php_usual_lib_suspects = PHP_PHP_BUILD+"\\lib";

ADD_FLAG("CFLAGS", '/I "' + php_usual_include_suspects + '" ');
ADD_FLAG("LDFLAGS", '/libpath:"' + php_usual_lib_suspects + '" ');
ADD_FLAG("ARFLAGS", '/nologo /libpath:"' + php_usual_lib_suspects + '" ');

probe_basic_headers();
add_extra_dirs();

//DEFINE("PHP_BUILD", PHP_PHP_BUILD);

ARG_WITH("analyzer", "Enable static analyzer. Pass vs for Visual Studio, clang for clang, cppcheck for Cppcheck, pvs for PVS-Studio", "no");
if (PHP_ANALYZER == "vs") {
	ADD_FLAG("CFLAGS", " /analyze ");
	ADD_FLAG("CFLAGS", " /wd6308 ");
} else if (PHP_ANALYZER == "clang") {
	var clang_cl = false;

	if (FSO.FileExists(PROGRAM_FILES + "\\LLVM\\bin\\clang-cl.exe")) {
		clang_cl = PROGRAM_FILES + "\\LLVM\\bin\\clang-cl.exe";
	} else if (FSO.FileExists(PROGRAM_FILESx86 + "\\LLVM\\bin\\clang-cl.exe")) {
		clang_cl = PROGRAM_FILESx86 + "\\LLVM\\bin\\clang-cl.exe";
	}

	if (!clang_cl) {
		if (false == PATH_PROG('clang-cl', null, 'CLANG_CL')) {
			WARNING("Couldn't find clang binaries, static analyze was disabled");
			PHP_ANALYZER = "no";
		}
	} else {
		DEFINE("CLANG_CL", clang_cl);
	}
} else if (PHP_ANALYZER == "cppcheck") {

	var cppcheck = false;

	if (FSO.FileExists(PROGRAM_FILES + "\\Cppcheck\\cppcheck.exe")) {
		cppcheck = PROGRAM_FILES + "\\Cppcheck\\cppcheck.exe";
	} else if (FSO.FileExists(PROGRAM_FILESx86 + "\\Cppcheck\\cppcheck.exe")) {
		cppcheck = PROGRAM_FILESx86 + "\\Cppcheck\\cppcheck.exe";
	}
	if (!cppcheck) {
		if (false == PATH_PROG('cppcheck', null, 'CPPCHECK')) {
			WARNING("Couldn't find Cppcheck binaries, static analyze was disabled");
			PHP_ANALYZER = "no";
		} else {
			cppcheck = get_define("CPPCHECK");
		}
	} else {
		DEFINE("CPPCHECK", cppcheck);
	}

	if (cppcheck) {
		var _tmp = execute(cppcheck + " --version").split(/ /)[1];
		var cppcheck_ver = [
			parseInt(_tmp.split(".")[0]),
			parseInt(_tmp.split(".")[1]),
		];
		if (cppcheck_ver[0] > 1 || cppcheck_ver[0] == 1 && cppcheck_ver[1] >= 77) {
			var build_dir = get_define("BUILD_DIR");
			var cppcheck_build_dir = build_dir + "\\cppcheck_build";
			if (!FSO.FolderExists(cppcheck_build_dir)) {
				FSO.CreateFolder(cppcheck_build_dir);
			}
			DEFINE("CPPCHECK_BUILD_DIR", cppcheck_build_dir);
		}
	}

} else if (PHP_ANALYZER == "pvs") {
	var pvs_studio = false;

	if (FSO.FileExists(PROGRAM_FILES + "\\PVS-Studio\\x64\\PVS-Studio.exe")) {
		pvs_studio = PROGRAM_FILES + "\\PVS-Studio\\x86\\PVS-Studio.exe";
	} else if (FSO.FileExists(PROGRAM_FILESx86 + "\\PVS-Studio\\x64\\PVS-Studio.exe")) {
		pvs_studio = PROGRAM_FILESx86 + "\\PVS-Studio\\x64\\PVS-Studio.exe";
	}

	if (!pvs_studio) {
		WARNING("Couldn't find PVS-Studio binaries, static analyze was disabled");
		PHP_ANALYZER = "no";
	} else {
		var pvscfg = FSO.CreateTextFile("PVS-Studio.conf", true);
		DEFINE("PVS_STUDIO", pvs_studio);

		pvscfg.WriteLine("exclude-path = " + VCINSTALLDIR);
		if (FSO.FolderExists(PROGRAM_FILESx86 + "\\windows kits\\")) {
			pvscfg.WriteLine("exclude-path = " + PROGRAM_FILESx86 + "\\windows kits\\");
		} else if (FSO.FolderExists(PROGRAM_FILES + "\\windows kits\\")) {
			pvscfg.WriteLine("exclude-path = " + PROGRAM_FILES + "\\windows kits\\");
		}
		pvscfg.WriteLine("vcinstalldir = " + VCINSTALLDIR);
		pvscfg.WriteLine("platform = " + (X64 ? 'x64' : 'Win32'));
		pvscfg.WriteLine("preprocessor = visualcpp");
		pvscfg.WriteLine("language = C");
		pvscfg.WriteLine("skip-cl-exe = no");
	}
} else {
	PHP_ANALYZER = "no"
}

STDOUT.WriteBlankLines(1);
STDOUT.WriteLine("Build dir: " + get_define('BUILD_DIR'));
STDOUT.WriteLine("PHP Core:  " + get_define('PHPDLL') + " and " + get_define('PHPLIB'));

ADD_SOURCES("Zend", "zend_language_parser.c zend_language_scanner.c \
	zend_ini_parser.c zend_ini_scanner.c zend_alloc.c zend_compile.c \
	zend_constants.c zend_exceptions.c \
	zend_execute_API.c zend_highlight.c \
	zend_llist.c zend_vm_opcodes.c zend_opcode.c zend_operators.c zend_ptr_stack.c \
	zend_stack.c zend_variables.c zend.c zend_API.c zend_extensions.c \
	zend_hash.c zend_list.c zend_builtin_functions.c \
	zend_ini.c zend_sort.c zend_multibyte.c zend_ts_hash.c \
	zend_stream.c zend_iterators.c zend_interfaces.c zend_objects.c \
	zend_object_handlers.c zend_objects_API.c \
	zend_default_classes.c zend_execute.c zend_strtod.c zend_gc.c zend_closures.c zend_weakrefs.c \
	zend_float.c zend_string.c zend_generators.c zend_virtual_cwd.c zend_ast.c \
	zend_inheritance.c zend_smart_str.c zend_cpuinfo.c");

ADD_FLAG("CFLAGS_BD_ZEND", "/D ZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
if (VS_TOOLSET && VCVERS >= 1914) {
	ADD_FLAG("CFLAGS_BD_ZEND", "/d2FuncCache1");
}

/* XXX inspect this for other toolsets */
//AC_DEFINE('ZEND_DVAL_TO_LVAL_CAST_OK', 1);

ADD_SOURCES("main", "main.c snprintf.c spprintf.c getopt.c fopen_wrappers.c \
	php_scandir.c php_ini.c SAPI.c rfc1867.c php_content_types.c strlcpy.c \
	strlcat.c mergesort.c reentrancy.c php_variables.c php_ticks.c network.c \
	php_open_temporary_file.c output.c internal_functions.c \
	php_syslog.c");
ADD_FLAG("CFLAGS_BD_MAIN", "/D ZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
if (VS_TOOLSET && VCVERS >= 1914) {
	ADD_FLAG("CFLAGS_BD_MAIN", "/d2FuncCache1");
}

AC_DEFINE('HAVE_STRNLEN', 1);

ADD_SOURCES("main/streams", "streams.c cast.c memory.c filter.c plain_wrapper.c \
	userspace.c transports.c xp_socket.c mmap.c glob_wrapper.c");
ADD_FLAG("CFLAGS_BD_MAIN_STREAMS", "/D ZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
if (VS_TOOLSET && VCVERS >= 1914) {
	ADD_FLAG("CFLAGS_BD_MAIN_STREAMS", "/d2FuncCache1");
}

ADD_SOURCES("win32", "dllmain.c glob.c readdir.c \
	registry.c select.c sendmail.c time.c winutil.c wsyslog.c globals.c \
	getrusage.c ftok.c ioutil.c codepage.c nice.c \
	inet.c fnmatch.c sockets.c console.c signal.c");

ADD_FLAG("CFLAGS_BD_WIN32", "/D ZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
if (VS_TOOLSET && VCVERS >= 1914) {
	ADD_FLAG("CFLAGS_BD_WIN32", "/d2FuncCache1");
}

PHP_INSTALL_HEADERS("", "Zend/ TSRM/ main/ main/streams/ win32/");

STDOUT.WriteBlankLines(1);


/* Can we build with IPv6 support? */
ARG_ENABLE("ipv6", "Disable IPv6 support (default is turn it on if available)", "yes");

var main_network_has_ipv6 = 0;
if (PHP_IPV6 == "yes") {
	main_network_has_ipv6 = CHECK_HEADER_ADD_INCLUDE("wspiapi.h", "CFLAGS") ? 1 : 0;
}
if (main_network_has_ipv6) {
	STDOUT.WriteLine("Enabling IPv6 support");
}
AC_DEFINE('HAVE_GETADDRINFO', main_network_has_ipv6);
AC_DEFINE('HAVE_GAI_STRERROR', main_network_has_ipv6);
AC_DEFINE('HAVE_IPV6', main_network_has_ipv6);

/* this allows up to 256 sockets to be select()ed in a single
 * call to select(), instead of the usual 64 */
ARG_ENABLE('fd-setsize', "Set maximum number of sockets for select(2)", "256");
ADD_FLAG("CFLAGS", "/D FD_SETSIZE=" + parseInt(PHP_FD_SETSIZE));

/* For snapshot builders, where can we find the additional
 * files that make up the snapshot template? */
ARG_WITH("snapshot-template", "Path to snapshot builder template dir", "no");

if (PHP_SNAPSHOT_TEMPLATE == "no") {
	/* default is as a sibling of the php_build dir */
	if (FSO.FolderExists(PHP_PHP_BUILD + "\\template")) {
		PHP_SNAPSHOT_TEMPLATE = FSO.GetAbsolutePathName(PHP_PHP_BUILD + "\\template");
	} else if (FSO.FolderExists(PHP_PHP_BUILD + "\\..\\template")) {
		PHP_SNAPSHOT_TEMPLATE = FSO.GetAbsolutePathName(PHP_PHP_BUILD + "\\..\\template");
	}
}

DEFINE('SNAPSHOT_TEMPLATE', PHP_SNAPSHOT_TEMPLATE);

ARG_ENABLE("security-flags", "Disable the compiler security flags", "yes");
if (PHP_SECURITY_FLAGS == "yes") {
	ADD_FLAG("LDFLAGS", "/NXCOMPAT /DYNAMICBASE ");
}

if (CLANG_TOOLSET) {
	ARG_WITH("uncritical-warn-choke", "Disable some uncritical warnings", "yes");
	if (PHP_UNCRITICAL_WARN_CHOKE != "no") {
		ADD_FLAG("CFLAGS", "-Wno-ignored-attributes -Wno-deprecated-declarations -Wno-missing-braces " +
		"-Wno-logical-op-parentheses -Wno-msvc-include -Wno-invalid-source-encoding -Wno-unknown-pragmas " +
		"-Wno-unused-command-line-argument -Wno-unused-function -Wno-ignored-pragma-optimize");
	}

	ARG_ENABLE("sanitizer", "Enable ASan and UBSan extensions", "no");
	if (PHP_SANITIZER == "yes") {
		if (COMPILER_NUMERIC_VERSION < 500) {
			ERROR("Clang at least 5.0.0 required for sanitation plugins");
		}
		add_asan_opts("CFLAGS", "LIBS", "LDFLAGS");
	}
}

ARG_WITH("codegen-arch", "Architecture for code generation: ia32. Use --enable-native-intrinsics to enable SIMD optimizations.", "no");
toolset_setup_codegen_arch();

ARG_WITH("all-shared", "Force all the non obligatory extensions to be shared", "no");

// Config profiles (--with-config-profile=<name>) will save a certain config to php-src/config.<name>.bat
// so that it can be executed like: cofig.<name> instead of a long list of parameters
//
// Note, nice as a name is disallowed and will generate a warning and skip saving
ARG_WITH('config-profile', 'Name of the configuration profile to save this to in php-src/config.name.bat', 'no');

ARG_ENABLE("test-ini", "Enable automatic php.ini generation. The test.ini will be put \
		into the build dir and used to automatically load the shared extensions.", "yes");

ARG_WITH("test-ini-ext-exclude", "Comma separated list of shared extensions to \
		be excluded from the test.ini", "no");

ARG_ENABLE("native-intrinsics", "Comma separated list of intrinsic optimizations to enable. \
	Available instruction set names are sse, sse2, sse3, ssse3, sse4.1, sse4.2, avx, avx2. \
	SSE and SSE2 are enabled by default. The best instruction set specified will \
	automatically enable all the older instruction sets. Note, that the produced binary \
	might not work properly, if the chosen instruction sets are not available on the target \
	processor.", "no");
toolset_setup_intrinsic_cflags();
