Repository: numpy/numpy

Issue:
Increased output when running f2py (possibly due to SIMD detection code)
In NumPy 1.20, when compiling extensions with f2py, there is a lot of compiler options/testing output that is not there in 1.19:

### Reproducing code example:

Saving `fib1.f` from [the documentation](https://numpy.org/devdocs/f2py/f2py.getting-started.html) and running

```bash
f2py -c -m fib1 fib1.f
```

results in several blocks similar to

```
compile options: '-I/opt/miniconda3/envs/numpy-dev/include/python3.8 -c'
extra options: '-msse2'
CCompilerOpt.feature_test[1448] : testing feature 'SSE' with flags (-msse -msse2)
C compiler: gcc -pthread -B /opt/miniconda3/envs/numpy-dev/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC
```

[Full output in a pastebin](https://pastebin.com/HrJgK9Km)

This also increases the compilation time as several of those tests are apparently executed during compilation.

### Numpy/Python version information:

```python
>>> import sys, numpy; print(numpy.__version__, sys.version)
1.20.0.dev0+087ad5e 3.8.3 | packaged by conda-forge | (default, Jun  1 2020, 17:43:00) 
[GCC 7.5.0]
```



Discussion:

Resolving patch:
diff --git a/numpy/distutils/command/build_clib.py b/numpy/distutils/command/build_clib.py
--- a/numpy/distutils/command/build_clib.py
+++ b/numpy/distutils/command/build_clib.py
@@ -259,57 +259,56 @@ def build_a_library(self, build_info, lib_name, libraries):
         if requiref90:
             self.mkpath(module_build_dir)
 
-        dispatch_objects = []
-        if not self.disable_optimization:
-            dispatch_sources  = [
-                c_sources.pop(c_sources.index(src))
-                for src in c_sources[:] if src.endswith(".dispatch.c")
-            ]
-            if dispatch_sources:
-                if not self.inplace:
-                    build_src = self.get_finalized_command("build_src").build_src
-                else:
-                    build_src = None
-                dispatch_objects = self.compiler_opt.try_dispatch(
-                    dispatch_sources,
-                    output_dir=self.build_temp,
-                    src_dir=build_src,
-                    macros=macros,
-                    include_dirs=include_dirs,
-                    debug=self.debug,
-                    extra_postargs=extra_postargs
-                )
-            extra_args_baseopt = extra_postargs + self.compiler_opt.cpu_baseline_flags()
-        else:
-            extra_args_baseopt = extra_postargs
-            macros.append(("NPY_DISABLE_OPTIMIZATION", 1))
-
         if compiler.compiler_type == 'msvc':
             # this hack works around the msvc compiler attributes
             # problem, msvc uses its own convention :(
             c_sources += cxx_sources
             cxx_sources = []
 
+        # filtering C dispatch-table sources when optimization is not disabled,
+        # otherwise treated as normal sources.
+        copt_c_sources = []
+        copt_baseline_flags = []
+        copt_macros = []
+        if not self.disable_optimization:
+            copt_build_src = None if self.inplace else self.get_finalized_command("build_src").build_src
+            copt_c_sources = [
+                c_sources.pop(c_sources.index(src))
+                for src in c_sources[:] if src.endswith(".dispatch.c")
+            ]
+            copt_baseline_flags = self.compiler_opt.cpu_baseline_flags()
+        else:
+            copt_macros.append(("NPY_DISABLE_OPTIMIZATION", 1))
+
         objects = []
+        if copt_c_sources:
+            log.info("compiling C dispatch-able sources")
+            objects += self.compiler_opt.try_dispatch(copt_c_sources,
+                                                      output_dir=self.build_temp,
+                                                      src_dir=copt_build_src,
+                                                      macros=macros + copt_macros,
+                                                      include_dirs=include_dirs,
+                                                      debug=self.debug,
+                                                      extra_postargs=extra_postargs)
+
         if c_sources:
             log.info("compiling C sources")
-            objects = compiler.compile(c_sources,
-                                       output_dir=self.build_temp,
-                                       macros=macros,
-                                       include_dirs=include_dirs,
-                                       debug=self.debug,
-                                       extra_postargs=extra_args_baseopt)
-        objects.extend(dispatch_objects)
+            objects += compiler.compile(c_sources,
+                                        output_dir=self.build_temp,
+                                        macros=macros + copt_macros,
+                                        include_dirs=include_dirs,
+                                        debug=self.debug,
+                                        extra_postargs=extra_postargs + copt_baseline_flags)
 
         if cxx_sources:
             log.info("compiling C++ sources")
             cxx_compiler = compiler.cxx_compiler()
             cxx_objects = cxx_compiler.compile(cxx_sources,
                                                output_dir=self.build_temp,
-                                               macros=macros,
+                                               macros=macros + copt_macros,
                                                include_dirs=include_dirs,
                                                debug=self.debug,
-                                               extra_postargs=extra_postargs)
+                                               extra_postargs=extra_postargs + copt_baseline_flags)
             objects.extend(cxx_objects)
 
         if f_sources or fmodule_sources:
diff --git a/numpy/distutils/command/build_ext.py b/numpy/distutils/command/build_ext.py
--- a/numpy/distutils/command/build_ext.py
+++ b/numpy/distutils/command/build_ext.py
@@ -406,52 +406,49 @@ def build_extension(self, ext):
 
         include_dirs = ext.include_dirs + get_numpy_include_dirs()
 
-        dispatch_objects = []
+        # filtering C dispatch-table sources when optimization is not disabled,
+        # otherwise treated as normal sources.
+        copt_c_sources = []
+        copt_baseline_flags = []
+        copt_macros = []
         if not self.disable_optimization:
-            dispatch_sources  = [
+            copt_build_src = None if self.inplace else self.get_finalized_command("build_src").build_src
+            copt_c_sources = [
                 c_sources.pop(c_sources.index(src))
                 for src in c_sources[:] if src.endswith(".dispatch.c")
             ]
-            if dispatch_sources:
-                if not self.inplace:
-                    build_src = self.get_finalized_command("build_src").build_src
-                else:
-                    build_src = None
-                dispatch_objects = self.compiler_opt.try_dispatch(
-                    dispatch_sources,
-                    output_dir=output_dir,
-                    src_dir=build_src,
-                    macros=macros,
-                    include_dirs=include_dirs,
-                    debug=self.debug,
-                    extra_postargs=extra_args,
-                    **kws
-                )
-            extra_args_baseopt = extra_args + self.compiler_opt.cpu_baseline_flags()
+            copt_baseline_flags = self.compiler_opt.cpu_baseline_flags()
         else:
-            extra_args_baseopt = extra_args
-            macros.append(("NPY_DISABLE_OPTIMIZATION", 1))
+            copt_macros.append(("NPY_DISABLE_OPTIMIZATION", 1))
 
         c_objects = []
+        if copt_c_sources:
+            log.info("compiling C dispatch-able sources")
+            c_objects += self.compiler_opt.try_dispatch(copt_c_sources,
+                                                        output_dir=output_dir,
+                                                        src_dir=copt_build_src,
+                                                        macros=macros + copt_macros,
+                                                        include_dirs=include_dirs,
+                                                        debug=self.debug,
+                                                        extra_postargs=extra_args,
+                                                        **kws)
         if c_sources:
             log.info("compiling C sources")
-            c_objects = self.compiler.compile(c_sources,
-                                              output_dir=output_dir,
-                                              macros=macros,
-                                              include_dirs=include_dirs,
-                                              debug=self.debug,
-                                              extra_postargs=extra_args_baseopt,
-                                              **kws)
-        c_objects.extend(dispatch_objects)
-
+            c_objects += self.compiler.compile(c_sources,
+                                               output_dir=output_dir,
+                                               macros=macros + copt_macros,
+                                               include_dirs=include_dirs,
+                                               debug=self.debug,
+                                               extra_postargs=extra_args + copt_baseline_flags,
+                                               **kws)
         if cxx_sources:
             log.info("compiling C++ sources")
             c_objects += cxx_compiler.compile(cxx_sources,
                                               output_dir=output_dir,
-                                              macros=macros,
+                                              macros=macros + copt_macros,
                                               include_dirs=include_dirs,
                                               debug=self.debug,
-                                              extra_postargs=extra_args,
+                                              extra_postargs=extra_args + copt_baseline_flags,
                                               **kws)
 
         extra_postargs = []
diff --git a/numpy/f2py/f2py2e.py b/numpy/f2py/f2py2e.py
--- a/numpy/f2py/f2py2e.py
+++ b/numpy/f2py/f2py2e.py
@@ -650,7 +650,9 @@ def run_compile():
     sys.argv.extend(['build',
                      '--build-temp', build_dir,
                      '--build-base', build_dir,
-                     '--build-platlib', '.'])
+                     '--build-platlib', '.',
+                     # disable CCompilerOpt
+                     '--disable-optimization'])
     if fc_flags:
         sys.argv.extend(['config_fc'] + fc_flags)
     if flib_flags:


Test patch: