Skip to content

Commit 476d3a7

Browse files
authored
[mypyc] Refactor: Move compiler flag logic into a function (#20625)
1 parent 67b9c81 commit 476d3a7

File tree

1 file changed

+91
-53
lines changed

1 file changed

+91
-53
lines changed

mypyc/build.py

Lines changed: 91 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,89 @@ def mypyc_build(
545545
return groups, group_cfilenames, source_deps
546546

547547

548+
def get_cflags(
549+
*,
550+
compiler_type: str | None = None,
551+
opt_level: str = "3",
552+
debug_level: str = "1",
553+
multi_file: bool = False,
554+
experimental_features: bool = False,
555+
log_trace: bool = False,
556+
) -> list[str]:
557+
"""Get C compiler flags for the given configuration.
558+
559+
Args:
560+
compiler_type: Compiler type, e.g. "unix" or "msvc". If None, detected automatically.
561+
opt_level: Optimization level as string ("0", "1", "2", or "3").
562+
debug_level: Debug level as string ("0", "1", "2", or "3").
563+
multi_file: Whether multi-file compilation mode is enabled.
564+
experimental_features: Whether experimental features are enabled.
565+
log_trace: Whether trace logging is enabled.
566+
567+
Returns:
568+
List of compiler flags.
569+
"""
570+
if compiler_type is None:
571+
compiler: Any = ccompiler.new_compiler()
572+
sysconfig.customize_compiler(compiler)
573+
compiler_type = compiler.compiler_type
574+
575+
cflags: list[str] = []
576+
if compiler_type == "unix":
577+
cflags += [
578+
f"-O{opt_level}",
579+
f"-g{debug_level}",
580+
"-Werror",
581+
"-Wno-unused-function",
582+
"-Wno-unused-label",
583+
"-Wno-unreachable-code",
584+
"-Wno-unused-variable",
585+
"-Wno-unused-command-line-argument",
586+
"-Wno-unknown-warning-option",
587+
"-Wno-unused-but-set-variable",
588+
"-Wno-ignored-optimization-argument",
589+
# Disables C Preprocessor (cpp) warnings
590+
# See https://github.com/mypyc/mypyc/issues/956
591+
"-Wno-cpp",
592+
]
593+
if log_trace:
594+
cflags.append("-DMYPYC_LOG_TRACE")
595+
if experimental_features:
596+
cflags.append("-DMYPYC_EXPERIMENTAL")
597+
if opt_level == "0":
598+
cflags.append("-UNDEBUG")
599+
elif compiler_type == "msvc":
600+
# msvc doesn't have levels, '/O2' is full and '/Od' is disable
601+
if opt_level == "0":
602+
opt_level = "d"
603+
cflags.append("/UNDEBUG")
604+
elif opt_level in ("1", "2", "3"):
605+
opt_level = "2"
606+
if debug_level == "0":
607+
debug_level = "NONE"
608+
elif debug_level == "1":
609+
debug_level = "FASTLINK"
610+
elif debug_level in ("2", "3"):
611+
debug_level = "FULL"
612+
cflags += [
613+
f"/O{opt_level}",
614+
f"/DEBUG:{debug_level}",
615+
"/wd4102", # unreferenced label
616+
"/wd4101", # unreferenced local variable
617+
"/wd4146", # negating unsigned int
618+
]
619+
if multi_file:
620+
# Disable whole program optimization in multi-file mode so
621+
# that we actually get the compilation speed and memory
622+
# use wins that multi-file mode is intended for.
623+
cflags += ["/GL-", "/wd9025"] # warning about overriding /GL
624+
if log_trace:
625+
cflags.append("/DMYPYC_LOG_TRACE")
626+
if experimental_features:
627+
cflags.append("/DMYPYC_EXPERIMENTAL")
628+
return cflags
629+
630+
548631
def mypycify(
549632
paths: list[str],
550633
*,
@@ -659,59 +742,14 @@ def mypycify(
659742

660743
build_dir = compiler_options.target_dir
661744

662-
cflags: list[str] = []
663-
if compiler.compiler_type == "unix":
664-
cflags += [
665-
f"-O{opt_level}",
666-
f"-g{debug_level}",
667-
"-Werror",
668-
"-Wno-unused-function",
669-
"-Wno-unused-label",
670-
"-Wno-unreachable-code",
671-
"-Wno-unused-variable",
672-
"-Wno-unused-command-line-argument",
673-
"-Wno-unknown-warning-option",
674-
"-Wno-unused-but-set-variable",
675-
"-Wno-ignored-optimization-argument",
676-
# Disables C Preprocessor (cpp) warnings
677-
# See https://github.com/mypyc/mypyc/issues/956
678-
"-Wno-cpp",
679-
]
680-
if log_trace:
681-
cflags.append("-DMYPYC_LOG_TRACE")
682-
if experimental_features:
683-
cflags.append("-DMYPYC_EXPERIMENTAL")
684-
if opt_level == "0":
685-
cflags.append("-UNDEBUG")
686-
elif compiler.compiler_type == "msvc":
687-
# msvc doesn't have levels, '/O2' is full and '/Od' is disable
688-
if opt_level == "0":
689-
opt_level = "d"
690-
cflags.append("/UNDEBUG")
691-
elif opt_level in ("1", "2", "3"):
692-
opt_level = "2"
693-
if debug_level == "0":
694-
debug_level = "NONE"
695-
elif debug_level == "1":
696-
debug_level = "FASTLINK"
697-
elif debug_level in ("2", "3"):
698-
debug_level = "FULL"
699-
cflags += [
700-
f"/O{opt_level}",
701-
f"/DEBUG:{debug_level}",
702-
"/wd4102", # unreferenced label
703-
"/wd4101", # unreferenced local variable
704-
"/wd4146", # negating unsigned int
705-
]
706-
if multi_file:
707-
# Disable whole program optimization in multi-file mode so
708-
# that we actually get the compilation speed and memory
709-
# use wins that multi-file mode is intended for.
710-
cflags += ["/GL-", "/wd9025"] # warning about overriding /GL
711-
if log_trace:
712-
cflags.append("/DMYPYC_LOG_TRACE")
713-
if experimental_features:
714-
cflags.append("/DMYPYC_EXPERIMENTAL")
745+
cflags = get_cflags(
746+
compiler_type=compiler.compiler_type,
747+
opt_level=opt_level,
748+
debug_level=debug_level,
749+
multi_file=multi_file,
750+
experimental_features=experimental_features,
751+
log_trace=log_trace,
752+
)
715753

716754
# If configured to (defaults to yes in multi-file mode), copy the
717755
# runtime library in. Otherwise it just gets #included to save on

0 commit comments

Comments
 (0)