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

Add CMake build system #220

Closed
wants to merge 43 commits into from
Closed

Add CMake build system #220

wants to merge 43 commits into from

Conversation

Youw
Copy link
Member

@Youw Youw commented Dec 11, 2020

This is still work-in-progress. A draft PR, so can be reviewed by community.

See: #288.

@Youw Youw marked this pull request as draft December 11, 2020 19:03
@Youw Youw added the build system/CI Anything related to building the project or running on CI label Dec 11, 2020
This was referenced Dec 11, 2020
Copy link
Contributor

@Qix- Qix- left a comment

Choose a reason for hiding this comment

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

Going to keep it at what I have now so it's not too overwhelming. Let me know if you'd like me to keep going or not :)

CMakeLists.txt Show resolved Hide resolved
CMakeLists.txt Outdated
Comment on lines 4 to 8
if(NOT DEFINED CMAKE_BUILD_TYPE OR CMAKE_BUILD_TYPE STREQUAL "")
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Debug;Release;MinSizeRel;RelWithDebInfo" FORCE)
endif()
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")

Copy link
Contributor

Choose a reason for hiding this comment

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

Don't modify this; it isn't idiomatic CMake and isn't necessary. If anything, give a warning about it not being set. In almost all cases, the user invoking cmake should specify it - e.g. the following is muscle memory for me personally:

mkdir build && cd build
cmake .. -GNinja -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=ON
Suggested change
if(NOT DEFINED CMAKE_BUILD_TYPE OR CMAKE_BUILD_TYPE STREQUAL "")
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Debug;Release;MinSizeRel;RelWithDebInfo" FORCE)
endif()
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
if(NOT DEFINED CMAKE_BUILD_TYPE OR CMAKE_BUILD_TYPE STREQUAL "")
message(WARNING "CMAKE_BUILD_TYPE not specified; HIDAPI might get mis-configured.")
endif()

Copy link
Member Author

Choose a reason for hiding this comment

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

so instead of type less, you suggest always type more (and possible type more errors)
I vote for user-friendly

muscle memory for me personally

and it makes it harder to use for those, who don't have such muscle memory


even more: I use cmake-gui even more often then a command-line. It saves me much time. And it saves me time to pick a selection from a combo-box, instead of typing it manually each time.

Copy link
Contributor

@Qix- Qix- Dec 11, 2020

Choose a reason for hiding this comment

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

so instead of type less, you suggest always type more

In this case, yes. It's not about typing less/more. It's about not dictating how users use your package.

Further, most people that will be using this will be either installing from a package manager or using it as a submodule - and only the latter would require someone typing it once to configure it.

and it makes it harder to use for those, who don't have such muscle memory

The point wasn't that I'm better. The point is that it's a pretty standard way to use CMake.

even more: I use cmake-gui even more often then a command-line

That is not the majority case.

Copy link
Member Author

Choose a reason for hiding this comment

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

or using it as a submodule - and only the latter would require someone typing it once to configure it.

And what problem does it make? Does it limit the users?

The point wasn't that I'm better.

Neither was mine.

The point is that it's a pretty standard way to use CMake.

For all my "small" project I have a different muscle memory: cmake .. && make -j. Pretty standard to me (and I didn't invented it).

Copy link
Contributor

Choose a reason for hiding this comment

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

The point here is that this is not common. Another user now has asked about it; you generally don't set these things, because debug/release isn't binary. I've rarely, hardly ever actually, seen a config that does this. It's the general case yo specify the config type you want to build for.

CMakeLists.txt Outdated Show resolved Hide resolved
CMakeLists.txt Show resolved Hide resolved
CMakeLists.txt Outdated Show resolved Hide resolved
hidtest/CMakeLists.txt Show resolved Hide resolved
hidtest/CMakeLists.txt Show resolved Hide resolved
hidtest/CMakeLists.txt Show resolved Hide resolved
hidtest/CMakeLists.txt Show resolved Hide resolved
libusb/CMakeLists.txt Show resolved Hide resolved
@Qix-
Copy link
Contributor

Qix- commented Dec 11, 2020

By the way, Travis removed support for open source projects as of this last November. You might want to consider moving entirely to sourcehut (since I see you're already using it and it's a fine option!) or to Github Actions/Circle/etc.

The travis jobs will probably never complete and thus stay yellow :)

@Youw
Copy link
Member Author

Youw commented Dec 11, 2020

Travis removed support for open source projects as of this last November

Fortunately, that is not true.
They are just migrating all Open Source projects from travis-ci.org to travis-ci.com. Check out their open letter. And of course, it remains free for Open Source as it was before.

@Youw
Copy link
Member Author

Youw commented Dec 11, 2020

Let me know if you'd like me to keep going or not :)

I guess I'd rather finish with the documentation first, so we can avoid my answers like "this is by current design".
This whole implementation is designed to make the usage simple, not the implementation simple.

@Youw
Copy link
Member Author

Youw commented Dec 11, 2020

The travis jobs will probably never complete and thus stay yellow :)

This is a good one, thanks. I'll switch to travis-ci.com. I though I already did it.

@Qix-
Copy link
Contributor

Qix- commented Dec 11, 2020

They are just migrating

Huh, I read something entirely different on HN. I guess that's good news, then.

CMakeLists.txt Outdated
Comment on lines 4 to 7
if(NOT DEFINED CMAKE_BUILD_TYPE OR CMAKE_BUILD_TYPE STREQUAL "")
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Debug;Release;MinSizeRel;RelWithDebInfo" FORCE)
endif()
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the point of this? Why don't you leave it to CMake to set the default?

Copy link
Member Author

Choose a reason for hiding this comment

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

What is the point of this?

It is for convenience of those, who are building hidapi as a standalone package/library. Try cmake-gui and you'll see.

Why don't you leave it to CMake to set the default?

Because by default CMake doesn't set it at all. Maybe it does makes sence somtime, but I haven't seen a single real project where it would.


As for the convenience part: see my other comment #220 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

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

I still don't get the point of overriding CMake's default behavior, but I guess it doesn't hurt? 🤷 It'll just be a bit of confusing code for people reading this in the future.

Copy link
Member Author

Choose a reason for hiding this comment

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

but I guess it doesn't hurt?

That's my point.

CMake's default behavior

Humor me. Can you tell what's CMake's default behavior if you don't specify CMAKE_BUILD_TYPE at all?

Copy link
Contributor

Choose a reason for hiding this comment

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

@Youw
There are multi-config generators (like MSVC) that ignores CMAKE_BUILD_TYPE. The value of this variable is also not limited to the ones you've listed - for example I use also TSan and ASan build type for sanitizers. I agree this part of code isn't necessary here but I don't think it will break something on the other hand.

Copy link
Member Author

Choose a reason for hiding this comment

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

ignores CMAKE_BUILD_TYPE

The problem that it is not exactly ignored. It is still available in the CMake scripts, and what bothers me the most: CMake doesn't default it to empty value when MSVC generator is used, but rather to "Debug" (gcc + make - it is defaulted to empy value).
I find such behavior way more confusing, but I guess CMake's developers must have had their reasons for that.

I use also TSan and ASan build type for sanitizers

I must admit - I haven't seen any custom CMAKE_BUILD_TYPE usage/implementation, so I guess that is a good point too.
Can you point to me a reference how your/typical usage look like?

As I mentioned earlier, a standalone build likely not to be used with a non-standard build types but I agree that prohibiting it wouldn't be a good thing, so there is that.

CMakeLists.txt Show resolved Hide resolved
CMakeLists.txt Outdated Show resolved Hide resolved
@sleiner
Copy link

sleiner commented Jun 10, 2021

I just tested this in two setups and it works great 👍🏻 Is anything holding back the merge? Is there anything I could do to help? 😊

@mcuee
Copy link
Member

mcuee commented Jun 16, 2021

I like this CMake improvement as well. Thanks.

@mcuee
Copy link
Member

mcuee commented Jun 18, 2021

It seems to work fine under my Mac Mini M1 (macOS Big Sur ARM64).

hidapi_cmake/build on  cmake-support ❯ cmake ..
-- The C compiler identification is AppleClang 12.0.5.12050022
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE  
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/mcuee/build/hidapi/hidapi_cmake/build

hidapi_cmake/build on  cmake-support [?] via △ v3.20.3 ❯ vim CMakeCache.txt 
(to enable hidtest and framwork build, proper way is to define in the command line).

hidapi_cmake/build on  cmake-support [?] via △ v3.20.3 took 1m27s ❯ cmake ..
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/mcuee/build/hidapi/hidapi_cmake/build

hidapi_cmake/build on  cmake-support [?] via △ v3.20.3 ❯ make
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/mcuee/build/hidapi/hidapi_cmake/build
[ 25%] Building C object src/mac/CMakeFiles/hidapi_darwin.dir/hid.c.o
[ 50%] Linking C shared library hidapi.framework/hidapi
Copying OS X content src/mac/hidapi.framework/Versions/0/Headers/hidapi.h
[ 50%] Built target hidapi_darwin
[ 75%] Building C object hidtest/CMakeFiles/hidtest.dir/test.c.o
[100%] Linking C executable hidtest
[100%] Built target hidtest

@Be-ing
Copy link
Contributor

Be-ing commented Jun 19, 2021

What is left to do here?

@Youw
Copy link
Member Author

Youw commented Jun 19, 2021

https://github.com/libusb/hidapi/pull/220/files#diff-0bc78e3e3203f9abc8d4db2b1e58170a5948f03e807325064aead14f8b310cc2R8

Working on it right now. Will finish today/tomorrow.
When I do - I'll squash all changes into two commits (one for CMake and one for Documentation update) and will open a new PR. Simply not to break this branch if someone is already using it.

@Youw
Copy link
Member Author

Youw commented Jun 19, 2021

It seems to work fine under my Mac Mini M1 (macOS Big Sur ARM64)

Thanks for testing it. I don't have M1 mac to check it myself. Even though there is nothing platform/architecture-specific that could suggest that the build could have failed, having a confirmation is always a good thing.

@Youw Youw mentioned this pull request Jun 21, 2021
BUILD.cmake.md Outdated Show resolved Hide resolved
BUILD.cmake.md Show resolved Hide resolved
BUILD.cmake.md Outdated Show resolved Hide resolved
CMakeLists.txt Outdated Show resolved Hide resolved
BUILD.cmake.md Outdated
Comment on lines 32 to 40
```sh
cd <build dir>
# configure the build
cmake -GNinja <HIDAPI source dir>
cmake <HIDAPI source dir>
# build it!
cmake --build .
# install library; by default installs into /usr/local/
cmake --build . --target install
# NOTE: you need to run install command as root, to be able to install into /usr/local/
Copy link
Contributor

@Be-ing Be-ing Jun 21, 2021

Choose a reason for hiding this comment

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

This would use the source tree as the build directory.

cmake -S . -B build

creates the directory build if it does not exist, then

cmake --build build
cmake --install build

Copy link
Member Author

Choose a reason for hiding this comment

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

This would use the source tree as the build directory.

Of course it wouldn't. It will create a build tree under current directory, which by cd <build dir> is any directory on the filesystem.

cmake -S . -B build

This creates a build directory inside the source tree, which by my experience is almost as bad as using source tree for build tree.

I'm using a software that scans all files under the source tree to index symbols (automatically) and if the build directory is under source tree - it scans it too, during the build as the build goes... So I prefer (and always recommend) to move build directorry somewhere outside the source tree completely.

@Youw Youw closed this in #288 Jul 3, 2021
@Youw Youw deleted the cmake-support branch July 3, 2021 17:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build system/CI Anything related to building the project or running on CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants