-
Notifications
You must be signed in to change notification settings - Fork 81
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
Conversation
@@ -32,14 +32,16 @@ endif() | |||
set (BUILD_DOCS ON CACHE BOOL "Generate doxygen-based documentation") | |||
|
|||
# CXX FILAGS | |||
if (NOT CMAKE_BUILD_TYPE) |
There was a problem hiding this comment.
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 也行
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我发现 debian 好像默认是用 RelWithDebInfo 发布的,等下测试一下
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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}) |
There was a problem hiding this comment.
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 基础上增加破坏语言标准的优化) 吗?
@Groveer 你看看是不是这样子的,如果是的话很多cmake都要改动呢 |
Doc Check bot |
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") |
There was a problem hiding this comment.
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 上没法用吧
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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 再加吧
ddd379c
to
dfc88a8
Compare
CMAKE_BUILD_TYPE |
即使 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 这里有一些讨论 |
ebd9e51
to
187a7cd
Compare
Log: Optimize compilation parameters
/merge |
Log: Optimize compilation parameters
Ref: https://deepin-community.github.io/sig-dde-porting/posts/compile_flag