Created
May 4, 2023 14:41
-
-
Save Meltwin/1ee35296d2bb86fee19d639580e3c91f to your computer and use it in GitHub Desktop.
Change all the CMakeLists.txt policy regarding the C++ standard to use C++17
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from colorama import Fore, Style | |
WANTED_WIDTH = 100 | |
DISPLAY_WIDTH = min(WANTED_WIDTH, os.get_terminal_size().columns - 2) | |
print("┏" + "".center(DISPLAY_WIDTH, "━") + "┓") | |
print(f"┃{Fore.RED}" + "CMakeLists to C++17 Utils".center(DISPLAY_WIDTH, " ") + f"{Style.RESET_ALL}┃") | |
print(f"┃{Style.BRIGHT+ Fore.BLACK}" + "Meltwin - 2023".center(DISPLAY_WIDTH, " ") + f"{Style.RESET_ALL}┃") | |
print("┗" + "".center(DISPLAY_WIDTH, "━") + "┛") | |
print() | |
REPLACE_FILTER = { | |
"-std=c++11":"-std=c++17", | |
"COMPILER_SUPPORTS_CXX11": "COMPILER_SUPPORTS_CXX17", | |
"CMAKE_CXX_STANDARD 11": "CMAKE_CXX_STANDARD 17", | |
"CMAKE_CXX_STANDARD 14": "CMAKE_CXX_STANDARD 17" | |
} | |
def fix_cmakelist(cmakelist_path: str) -> None: | |
print(f"{Fore.YELLOW}▷ {Fore.RESET}Found {Fore.RED}{cmakelist_path}{Fore.RESET}", end=" ") | |
with open(cmakelist_path, "r") as handle: | |
data = handle.read() | |
# Replacing | |
changed = False | |
for key, value in REPLACE_FILTER.items(): | |
if data.find(key) != -1: | |
data = data.replace(key, value) | |
changed = True | |
if changed: | |
with open(cmakelist_path, "w") as handle: | |
handle.write(data) | |
print(f"{Fore.GREEN}Fixed !" if changed else f"{Fore.BLUE}Nothing to change") | |
def walk_dir(dir: str, depth = 0) -> None: | |
if depth >=2: | |
return | |
for d in os.listdir(dir): | |
cmakelist_path = f"{dir}/{d}/CMakeLists.txt" | |
if not os.path.isfile(cmakelist_path): | |
walk_dir(f"{dir}/{d}", depth+1) | |
else : | |
fix_cmakelist(cmakelist_path) | |
if __name__ == "__main__": | |
# Get all the CMakeLists.txt | |
walk_dir("./src") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment