The detect_modules() method of setup.py became longer and longer over the years. It is now 1128 lines long. It's way too long: it becomes very hard to track the lifetime of a variable and many variables are overriden on purpose or not. Shorter functions help to track the lifetime of variables, ease review and reduce the number of bugs. Changes: * Fix detect_ctypes(): it no longer modifies global inc_dirs on macOS * self.srcdir is now always an absolute path * Add MACOS and MS_WINDOWS constants * Add TEST_EXTENSIONS to allow to not build extensions used to test Python (ex: _testcapi) * Add MATH_LIBS which is the libm library Other refactoring changes: * Reorder imports * Replace "from distutils.errors import *" with "from distutils.errors import CCompilerError, DistutilsError" to be able to use static analyzers like pyflakes * Rename globals to upper case to better distinguish if a variable is global or locale: * Rename cross_compiling to CROSS_COMPILING * Rename host_platform to HOST_PLATFORM * Rename disabled_module_list to DISABLED_MODULE_LIST * Don't run code at module top level, but at the beginning of main() * Move 'missing' and 'srcdir' from detect_modules() local variable to PyBuildExt attributes * Remove exts local variable from detect_modules(): add add_ext() method which append directly the module to self.extensions. * _detect_openssl() and _detect_nis() now directly add the extension and have no return value. * Split very long detect_modules() method into subfunctions, add many "detect_xxx" methods.