CMake function to add all subdirectories
Rather than doing this as part of generating the files with bazel_to_cmake, we can just have each file add all its subdirectories. Only adds directories that have a CMakeLists.txt file (same as the current bazel_to_cmake logic).
Depends on https://github.com/google/iree/pull/1003
This has a few advantages:
1. We don't have to remember to update the CMakeLists file (or rerun bazel_to_cmake) when adding a directory.
2. Any directory's CMakeLists file is dependent only on the corresponding BUILD file (plus the previous version of the CMakeLists for instructions like "DO NOT EDIT" and the correct license year).
3. As a consequence of (2), there is no ordering dependence on the directories processed by bazel_to_cmake, so we can always only run it on directory's with modified BUILD files.
4. This makes it possible to integrate bazel_to_cmake as a hermetic genrule with in bazel (with a few additional tweaks).
It shifts more work onto the cmake side (away from generation), however. To make sure it doesn't increase build times as a result, I tested out the comparative build times on my workstation. There is no substantial difference. Also it still runs the same number of tests (and they all pass).
Configure only:
```shell
$ rm -rf build/ && mkdir build && cd build && time cmake -G Ninja -DCMAKE_BUILD_TYPE=FastBuild -DIREE_BUILD_COMPILER=ON -DIREE_BUILD_TESTS=ON -DIREE_BUILD_SAMPLES=OFF -DIREE_BUILD_DEBUGGER=OFF ..
```
Before:
```shell
real 0m25.487s
user 0m17.906s
sys 0m7.606s
```
After:
```shell
real 0m25.418s
user 0m17.844s
sys 0m7.599s
```
Clean build:
```shell
$ time ./build_tools/cmake/clean_build.sh
```
Before:
```shell
real 6m36.789s
user 63m54.858s
sys 3m52.994s
```
After:
```shell
real 6m38.352s
user 63m55.938s
sys 3m50.222s
```
Rebuild (no changes):
```shell
$ time ./build_tools/cmake/rebuild.sh
```
Before:
```shell
real 1m36.174s
user 13m7.361s
sys 1m2.658s
```
After:
```shell
real 1m35.235s
user 13m5.488s
sys 1m3.178s
```
Test
```shell
$ time ./build_tools/cmake/test.sh
```
Before:
```shell
100% tests passed, 0 tests failed out of 211
Total Test time (real) = 4.10 sec
real 0m4.140s
user 0m16.732s
sys 0m13.647s
```
After:
```shell
100% tests passed, 0 tests failed out of 211
Total Test time (real) = 4.04 sec
real 0m4.089s
user 0m16.738s
sys 0m13.516s
```
Closes https://github.com/google/iree/pull/1002
COPYBARA_INTEGRATE_REVIEW=https://github.com/google/iree/pull/1002 from GMNGeoffrey:bazel-to-cmake 8c38f884ada0a83b038304dc9bbc7652c9487f39
PiperOrigin-RevId: 299888098
diff --git a/build_tools/bazel_to_cmake/bazel_to_cmake.py b/build_tools/bazel_to_cmake/bazel_to_cmake.py
index 49cc208..80c4772 100755
--- a/build_tools/bazel_to_cmake/bazel_to_cmake.py
+++ b/build_tools/bazel_to_cmake/bazel_to_cmake.py
@@ -574,18 +574,9 @@
self.first_error = None
def convert(self, copyright_line):
- # One `add_subdirectory(name)` per subdirectory.
- add_subdir_commands = []
- for root, dirs, _ in os.walk(self.directory_path):
- for d in sorted(dirs):
- if os.path.isfile(os.path.join(root, d, "CMakeLists.txt")):
- add_subdir_commands.append("add_subdirectory(%s)" % (d,))
- # Stop walk, only add direct subdirectories.
- break
-
converted_file = self.template % {
"copyright_line": copyright_line,
- "add_subdirectories": "\n".join(add_subdir_commands),
+ "add_subdirectories": "iree_add_all_subdirs()",
"body": self.body,
}
@@ -626,9 +617,7 @@
def convert_directory_tree(root_directory_path, write_files, strict):
print("convert_directory_tree: %s" % (root_directory_path,))
- # Process directories starting at leaves so we can skip add_directory on
- # subdirs without a CMakeLists file.
- for root, _, _ in os.walk(root_directory_path, topdown=False):
+ for root, _, _ in os.walk(root_directory_path):
convert_directory(root, write_files, strict)