Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Optimize compilation parameters #114

Merged
merged 1 commit into from
Oct 9, 2022
Merged

Conversation

wineee
Copy link
Member

@wineee wineee commented Sep 26, 2022

@@ -32,14 +32,16 @@ endif()
set (BUILD_DOCS ON CACHE BOOL "Generate doxygen-based documentation")

# CXX FILAGS
if (NOT CMAKE_BUILD_TYPE)
Copy link
Member Author

@wineee wineee Sep 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

设置默认 CMAKE_BUILD_TYPE 为 Release, 我们的 debian rules 都没有设置这个参数,反而 arch,nix 这些移植的发行版设置了
这里设置一下默认,不改 debian 也行

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我发现 debian 好像默认是用 RelWithDebInfo 发布的,等下测试一下

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该不需要指定 cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html

The default value is often an empty string, but this is usually not desirable and one of the other standard build types is usually more appropriate.

如果编译时没有设置这个 environment variable,同时没有-D传参数设置,就会使用默认的空字符串,文档也说了这个不太好,应该用 one of the other standard build types(Debug, Release, RelWithDebInfo and MinSizeRel)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

随便说一下,debain 构建时默认会传入 None,不是 empty string,但效果和 empty string 一样,不会加任何(-O1,-O2...-g)参数,但 debain 会通过环境变量 CXXFLAG 传入 -O2 -g (效果和RelWithDebInfo一样)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall -Wextra")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--as-needed")
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -g -fsanitize=address -fno-omit-frame-pointer")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug 模式肯定有 -O0 -g 啊,不用自己加的

set(BUILD_TESTING ON)
else ()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Ofast")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

除了debug,还有 RelWithDebInfo 和 MinSizeRel,这里设置 -Ofast 会破坏这两个模式,比如 MinSizeRel 用 -Os 优化大小,-Ofast 会破坏这种优化

CMakeLists.txt Outdated
endif ()
string(REPLACE "-O3" "-Ofast" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})
Copy link
Member Author

@wineee wineee Sep 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果是 Release 模式设置 Ofast 最好这样写,另外,默认的 O3 已经是高级别优化了,有必要用 Ofast(在 O3 基础上增加破坏语言标准的优化) 吗?

@Decodetalkers
Copy link
Contributor

@Groveer 你看看是不是这样子的,如果是的话很多cmake都要改动呢

@deepin-bot
Copy link
Contributor

deepin-bot bot commented Sep 26, 2022

Doc Check bot
🟢 Document Coverage Check Passed!

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall -Wextra")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--as-needed")
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -g -fsanitize=address -fno-omit-frame-pointer")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还有一个问题,msvc 用的是 / 而不是 -, 比如 /O2, /g 这些 flag 在windons 上没法用吧

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那加判断好一些 if(LINUX) if(Win32) if (MSVC)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加了 if (NOT MSVC), g++ 和 clang 应该都能接受
至于 MSVC 特有参数,等有人适配 windows 再加吧

@wineee
Copy link
Member Author

wineee commented Sep 26, 2022

CMAKE_BUILD_TYPE
archlinux-build: Release
debian-build: None
deepin-build: None
build-deb: None

@wineee
Copy link
Member Author

wineee commented Sep 26, 2022

即使 debian rules 没有写,也会主动传 -DCMAKE_BUILD_TYPE=None 参数,同时给 CMAKE_CXX_FLAGS 传参 -g -O2 -fdebug-prefix-map=/build/dtkcore-1.0.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2

没有使用 RelWithDebInfo, debian 的 -g -O2 是构建时传的 CXXFLAGS

https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=730636 这里有一些讨论

Log: Optimize compilation parameters
@wineee
Copy link
Member Author

wineee commented Oct 9, 2022

/merge

@deepin-bot deepin-bot bot merged commit 8ce7d3e into linuxdeepin:master Oct 9, 2022
@wineee wineee deleted the flag branch October 9, 2022 07:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants