[flang] Build intrinsic .mod files in include/flang

The build was putting .mod files for intrinsic modules in
tools/flang/include/flang but the install puts them in include/flang,
as does the out-of-tree build. This confused things for the driver.
This change makes the build consistent with the install and simplifies
the flang script accordingly.

Also, clean up the cmake commands for building the .mod files.

Differential Revision: https://reviews.llvm.org/D98522

GitOrigin-RevId: 8e1c09ee5f8066918c6c91982395f6449f97f056
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4ea0569..33e5521 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -190,6 +190,7 @@
   include_directories(SYSTEM ${MLIR_INCLUDE_DIR})
   include_directories(SYSTEM ${MLIR_TABLEGEN_OUTPUT_DIR})
 endif()
+set(FLANG_INTRINSIC_MODULES_DIR ${CMAKE_BINARY_DIR}/include/flang)
 
 if(FLANG_BUILD_NEW_DRIVER)
     # TODO: Remove when libclangDriver is lifted out of Clang
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 7328afc..905d491 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -5,8 +5,6 @@
   FLANG_STANDALONE_BUILD
 )
 
-set(FLANG_INTRINSIC_MODULES_DIR ${FLANG_BINARY_DIR}/include/flang)
-
 set(FLANG_TOOLS_DIR ${FLANG_BINARY_DIR}/bin)
 
 configure_lit_site_cfg(
diff --git a/tools/f18/CMakeLists.txt b/tools/f18/CMakeLists.txt
index 41237fe..d845161 100644
--- a/tools/f18/CMakeLists.txt
+++ b/tools/f18/CMakeLists.txt
@@ -30,7 +30,6 @@
   "__fortran_type_info"
 )
 
-set(include ${FLANG_BINARY_DIR}/include/flang)
 target_include_directories(f18
   PRIVATE
   ${CMAKE_CURRENT_BINARY_DIR}
@@ -38,27 +37,24 @@
 
 # Create module files directly from the top-level module source directory
 foreach(filename ${MODULES})
+  set(base ${FLANG_INTRINSIC_MODULES_DIR}/${filename})
   if(${filename} MATCHES "__fortran_type_info")
     set(depends "")
   elseif(${filename} MATCHES "__fortran_builtins")
-    set(depends ${include}/__fortran_type_info.mod)
+    set(depends ${FLANG_INTRINSIC_MODULES_DIR}/__fortran_type_info.mod)
   else()
-    set(depends ${include}/__fortran_builtins.mod)
+    set(depends ${FLANG_INTRINSIC_MODULES_DIR}/__fortran_builtins.mod)
   endif()
-  add_custom_command(OUTPUT ${include}/${filename}.mod
-    COMMAND f18 -fsyntax-only -I${include}
-      ${FLANG_SOURCE_DIR}/module/${filename}.f90
-    WORKING_DIRECTORY ${include}
+  add_custom_command(OUTPUT ${base}.mod
+    COMMAND f18 -fsyntax-only ${FLANG_SOURCE_DIR}/module/${filename}.f90
+    WORKING_DIRECTORY ${FLANG_INTRINSIC_MODULES_DIR}
     DEPENDS f18 ${FLANG_SOURCE_DIR}/module/${filename}.f90 ${depends}
   )
-  add_custom_command(OUTPUT ${include}/${filename}.f18.mod
-    DEPENDS ${include}/${filename}.mod
-    COMMAND ${CMAKE_COMMAND} -E
-      copy ${include}/${filename}.mod ${include}/${filename}.f18.mod)
-  list(APPEND MODULE_FILES ${include}/${filename}.mod)
-  list(APPEND MODULE_FILES ${include}/${filename}.f18.mod)
-  install(FILES ${include}/${filename}.mod DESTINATION include/flang)
-  install(FILES ${include}/${filename}.f18.mod DESTINATION include/flang)
+  add_custom_command(OUTPUT ${base}.f18.mod
+    DEPENDS ${base}.mod
+    COMMAND ${CMAKE_COMMAND} -E copy ${base}.mod ${base}.f18.mod)
+  list(APPEND MODULE_FILES ${base}.mod ${base}.f18.mod)
+  install(FILES ${base}.mod ${base}.f18.mod DESTINATION include/flang)
 endforeach()
 
 add_custom_target(module_files ALL DEPENDS ${MODULE_FILES})
diff --git a/tools/f18/flang b/tools/f18/flang
index 7109cd7..8dda836 100644
--- a/tools/f18/flang
+++ b/tools/f18/flang
@@ -8,11 +8,7 @@
 #===------------------------------------------------------------------------===#
 
 wd=$(cd $(dirname "$0")/.. && pwd)
-module_dir=$wd/include/flang
-if [[ ! -d $module_dir ]]; then
-  module_dir=$wd/tools/flang/include/flang
-fi
-opts="-module-suffix .f18.mod -intrinsic-module-directory $module_dir"
+opts="-module-suffix .f18.mod -intrinsic-module-directory $wd/include/flang"
 if ! $wd/bin/f18 $opts "$@"
 then status=$?
      echo flang: in $PWD, f18 failed with exit status $status: $wd/bin/f18 $opts "$@" >&2