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

(Almost) complete CMake support added #97

Closed
wants to merge 22 commits into from
Closed

(Almost) complete CMake support added #97

wants to merge 22 commits into from

Conversation

krokoziabla
Copy link
Contributor

@krokoziabla krokoziabla commented Apr 3, 2019

I examined the contents of configure.ac and Makefile.am's and *.vcxproj's and wrote a suite of CMakeLists.txt according to them. Everybody is welcome to try it out and suggest any comments or ideas on how to improve it.

Tested on Windows (MSVC, cygwin+gcc), on Mac (Xcode+clang, make+clang), on Linux (Ubuntu, Arch, Gentoo, make or ninja + clang or gcc), FreeBSD (clang+make)

Things to consider:

  • OGG support will function after PR Cmake updates ogg#40 will have been merged
  • XMMS plugin doern't seem to build even by autotools (it cannot find glib.h header out of the box) so I didn't try hard to make CMake build it either
  • I don't have a PPC machine so PPC features wasn't added/tested
  • Tests in /test folder could be converted to C/C++ in order to become platform independent and integrated to CMake's test framework. At the moment only /src/test_libFLAC and /src/test_libFLAC++ are there and can run on any flatform

Fixes #43

@erikd
Copy link
Member

erikd commented Apr 3, 2019

The XMMS plugin support is of very low priority.

PPC support is also low priority. I try to keep it working and if it gets broken I expect someone with a PPC machine to help fix it.

Tests in /test folder could be converted to C/C++ in order to become platform independent

I have looked at that, and unfortunately it looks like a huge task. Not sure how the tests are currently built and run on Windows with the VS project.

@lvqcl
Copy link
Contributor

lvqcl commented Apr 4, 2019

Not sure how the tests are currently built and run on Windows with the VS project.

built: VS builds the following .exe files (along with flac.exe and metaflac.exe, of course):
test_cuesheet.exe
test_libFLAC++.exe
test_libFLAC.exe
test_picture.exe
test_seeking.exe
test_streams.exe

run: they don't, AFAICS

@krokoziabla
Copy link
Contributor Author

krokoziabla commented Apr 6, 2019

Well, the scripts in /test folder still can be used on Unix since CMake builds all the binaries which those scripts reference

@erikd
Copy link
Member

erikd commented Apr 6, 2019

Just ran (from the .travis.yaml file from libsndfile):

mkdir cmake-build && \
cd cmake-build && \
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS="-Wall -Wextra" &&\
make VERBOSE=1 && \
ctest -V

and got:

CMake Error at src/CMakeLists.txt:8 (find_package):
  By not providing "FindOGG.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "OGG", but
  CMake did not find one.

  Could not find a package configuration file provided by "OGG" with any of
  the following names:

    OGGConfig.cmake
    ogg-config.cmake

  Add the installation prefix of "OGG" to CMAKE_PREFIX_PATH or set "OGG_DIR"
  to a directory containing one of the above files.  If "OGG" provides a
  separate development package or SDK, be sure it has been installed.

@krokoziabla
Copy link
Contributor Author

krokoziabla commented Apr 7, 2019

@erikd You need either to add -DWITH_OGG=OFF flag or to build xiph/ogg#40 and install it somewhere and then add -DOGG_DIR= (-DOGG_DIR is needed only if OGG doesn't get into standard installation paths on UNIX)

BTW, -Wall -Wextra are enabled by default, as well as the others from configure.ac

@erikd
Copy link
Member

erikd commented Apr 7, 2019

@krokoziabla I have the ogg development libraries installed in the standard location. The Cmake build systems should be able to find them (like it does for libsndfile). The -DWITH_OGG parameter should only be needed to override the default or to provide a alternative location.

@erikd
Copy link
Member

erikd commented Apr 7, 2019

Ok, now it can find the Ogg libraries, but the full test suite is not being run. The full test suite takes about 30 minutes.

@krokoziabla
Copy link
Contributor Author

krokoziabla commented Apr 7, 2019

Oh, sorry, I should have used another temporary branch so that you didn't see my temporary commits. I needed some time to make my fix stable on all the platforms I have. Now it is ready

but the full test suite is not being run. The full test suite takes about 30 minutes.

Not sure I get your point. make test now only runs test_libFLAC and test_libFLAC++ since successfulness of these tests is determined by their exit codes and that is what CTest needs.

The other tests still can be run but for that one needs to run something like this:
cd /path/to/flac/build/tests && for script in $(ls /path/to/flac/sources/tests/*.sh); do $script; done.
That is why I said about converting the tests to C/C++ so that one could just run make test

@erikd
Copy link
Member

erikd commented Apr 7, 2019

Ok, let me know when you have this sorted.

@krokoziabla
Copy link
Contributor Author

Do you mean you suggest converting tests to C/C++ or what? FindOGG.cmake seems to work well now, I don't have anything else to do with it.

@erikd
Copy link
Member

erikd commented Apr 8, 2019

Do you mean you suggest converting tests to C/C++ or what?

I would not recommend that. Is there no other way to get the full test suite running?

@krokoziabla
Copy link
Contributor Author

Done. I've made those scripts run by make test but that only works on UNIX

@evpobr
Copy link
Contributor

evpobr commented Apr 12, 2019

@krokoziabla Why so many CMakeLists.txt files everywhere? One per target is enough, CMake is not Autotools.

@krokoziabla
Copy link
Contributor Author

At least 31 CMakeLists.txt flies out of 43 contain a target. Most of the rest contain some install() and add_test() commands.

In general my intention was to avoid creating a GOD-like CMakeLists.txt

@evpobr
Copy link
Contributor

evpobr commented Apr 12, 2019

Then you have another problem - too many targets. Visual Studio will not be happy to handle this.

@evpobr
Copy link
Contributor

evpobr commented Apr 12, 2019

Also, for Windows linking STATIC libraries is OK, what about other OSes? At least Linux requires -fPIC flags for static library if it is linked to shared library.

@evpobr
Copy link
Contributor

evpobr commented Apr 12, 2019

One more thing - FLAC uses math functions, so under Unix math library must be linked (-lm).

@krokoziabla
Copy link
Contributor Author

Then you have another problem - too many targets.

I guess you're speaking about two different things at one time - too many CMakeLists.txt vs. too many targets. I believe the number of targets cannot be reduced.

Visual Studio will not be happy to handle this.

Too abstract... You can try it yourself. I didn't experience any problems with VS while building this

Also, for Windows linking STATIC libraries is OK, what about other OSes? At least Linux requires -fPIC
flags for static library if it is linked to shared library.

If you look into the CMakeLists.txt for libFLAC and libFLAC++ you'll see that there're two variants of libraries built - static and shared

One more thing - FLAC uses math functions, so under Unix math library must be linked (-lm).

... and it is

@evpobr
Copy link
Contributor

evpobr commented Apr 12, 2019

One more thing - FLAC uses math functions, so under Unix math library must be linked (-lm).

Sorry, i found it.

@evpobr
Copy link
Contributor

evpobr commented Apr 12, 2019

If you look into the CMakeLists.txt for libFLAC and libFLAC++ you'll see that there're two variants of libraries built - static and shared

And both are linked to the same STATIC utf8 library.

@evpobr
Copy link
Contributor

evpobr commented Apr 12, 2019

I guess you're speaking about two different things at one time - too many CMakeLists.txt vs. too many targets. I believe the number of targets cannot be reduced.

No. I mean if you will remove bunch of useless STATIC libraries you will remove banch of useless CMake scripts. Simpler is better.

@krokoziabla
Copy link
Contributor Author

krokoziabla commented Apr 12, 2019

And both are linked to the same STATIC utf8 library.

No. Only executables link to utf8, not shared libFLAC or libFLAC++

No. I mean if you will remove bunch of useless STATIC libraries you will remove banch of useless CMake scripts. Simpler is better.

The structure of targets/libraries was inherited from exirting automake/*.vcxproj files. I think it is not my business to change this. Probably the product owner will reorganase the repository later if he or she finds it useful.

@evpobr
Copy link
Contributor

evpobr commented Apr 12, 2019

No. Only binaries link to utf8, not shared libFLAC or libFLAC++

I se now. Then OK.

@evpobr
Copy link
Contributor

evpobr commented Apr 12, 2019

No ABI version. This is critical for UNIX.

set_target_properties(FLAC PROPERTIES
        VERSION 8.3.11
        SOVERSION 8
)

@krokoziabla
Copy link
Contributor Author

Why is it 8.3.11? I cannot find it anywhere in the repo.

@erikd
Copy link
Member

erikd commented Apr 29, 2019

There also seems to be a problem running the tests one machines where the Ogg libs are not installed. Specifically it seems to hang at:

1: opening Ogg FLAC file... OK
1: testing FLAC__stream_decoder_init_ogg_FILE()... OK

I suspect this is an issue with FLAC__HAS_OGG being incorrectly defined.

Another issue seems to be the test output. With the autotool test output the output is like:

8: sine16-18 (--channels=2 --bps=16 -5 -e --disable-verbatim-subframes --disable-constant-subframes): encode... decode... compare... OK

whereas the CMake build output is:

8: sine16-18 (--channels=2 --bps=16 -5 -e --disable-verbatim-subframes --disable-constant-subframes): encode...
8: decode...
8: compare...
8: OK

Would be nice to have the CMake output the same as the autotool output.

@krokoziabla
Copy link
Contributor Author

So close!

I notice that the tests seemed to be compiled with DEBUG enabled. This results in some extra unneeded output being printed eg:

1: testing FLAC__stream_decoder_process_until_end_of_stream()... 0 ('This')... content... CPU info (x86-64):
1:   CMOV ....... Y
1:   MMX ........ Y
1:   SSE ........ Y
1:   SSE2 ....... Y
1:   SSE3 ....... Y
1:   SSSE3 ...... Y
1:   SSE41 ...... Y
1:   SSE42 ...... Y
1:   AVX ........ Y
1:   FMA ........ Y
1:   AVX2 ....... Y
1:   AVX OS sup . Y
1: OK

Without DEBUG enabled the CPU capabilities are not printed.

Are you sure you compiled a release build? If you add -DCMAKE_BUILD_TYPE=Release CMake will pass NDEBUG to gcc and this output will be suppressed. It works in my setup

@krokoziabla
Copy link
Contributor Author

There also seems to be a problem running the tests one machines where the Ogg libs are not installed. Specifically it seems to hang at:

1: opening Ogg FLAC file... OK
1: testing FLAC__stream_decoder_init_ogg_FILE()... OK

I suspect this is an issue with FLAC__HAS_OGG being incorrectly defined.

Another issue seems to be the test output. With the autotool test output the output is like:

8: sine16-18 (--channels=2 --bps=16 -5 -e --disable-verbatim-subframes --disable-constant-subframes): encode... decode... compare... OK

whereas the CMake build output is:

8: sine16-18 (--channels=2 --bps=16 -5 -e --disable-verbatim-subframes --disable-constant-subframes): encode...
8: decode...
8: compare...
8: OK

Would be nice to have the CMake output the same as the autotool output.

I've checked now that if there is no OGG on my Mac then I don't see FLAC__stream_decoder_init_ogg_FILE messages. What is the value of OGG_FOUND macro in your generated config.h?

As to those newlines - fixed.

@erikd
Copy link
Member

erikd commented Apr 30, 2019

Are you sure you compiled a release build?

Is it possible to make the release build the default? That is the way it is with autotools.

@krokoziabla
Copy link
Contributor Author

Are you sure you compiled a release build?

Is it possible to make the release build the default? That is the way it is with autotools.

Done

@erikd
Copy link
Member

erikd commented Apr 30, 2019

Looking good. Will merge this in the morning.

@krokoziabla
Copy link
Contributor Author

@cristianadam Thanks. Fixed

@MarcusJohnson91
Copy link

MarcusJohnson91 commented Apr 30, 2019

@evpobr Amen about Cmake not needing a shit ton of CmakeLists.txt files laying around.

I personally just have a /Projects folder that contains a single CmakeLists.txt, and the source is in ../libX/

then I use git's submodules to incorporate various libraries together into a bigger project.


Quick question, is there a way to have the user set the library type (in a shared/static sense, not debug/release)?

add_library requires either STATIC or SHARED and I'm unsure if there's an official way to do that or not.

@krokoziabla
Copy link
Contributor Author

@MarcusJohnson91 you can use set BUILD_SHARED_LIBS variable for that https://cmake.org/cmake/help/latest/command/add_library.html

@erikd
Copy link
Member

erikd commented Apr 30, 2019

As mentioned in #100 (comment) it would be nice to have something in the README explaining how to use CMake with Visual Studio.

@krokoziabla
Copy link
Contributor Author

Done. But I apologise for my English. It's not very fluent. Probably you'll want to edit this part afterwards

@evpobr
Copy link
Contributor

evpobr commented May 1, 2019

Done. But I apologise for my English. It's not very fluent. Probably you'll want to edit this part afterwards

@krokoziabla, I see OGG in documentation. It is not abbreviation, the correct name is Ogg. Just to be clear.

@Chocobo1
Copy link
Contributor

Chocobo1 commented May 1, 2019

Just saw this, I think it is a must (at least for me) to have a cmake option to change MSVC runtime settings without manually patching CMakeLists.txt.

https://stackoverflow.com/questions/14172856/compile-with-mt-instead-of-md-using-cmake
https://docs.microsoft.com/en-us/cpp/build/reference/md-mt-ld-use-run-time-library?view=vs-2019

@krokoziabla
Copy link
Contributor Author

@Chocobo1 If I'm not mistaken you can regulate that by passing /Mx flags to CMake in CMAKE_C_FLAGS_RELEASE or CMAKE_C_FLAGS_DEBUG variables. Does it work for you?

@evpobr
Copy link
Contributor

evpobr commented May 1, 2019

if (ENABLE_STATIC_RUNTIME)
	if (MSVC)
		foreach (flag_var
			CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
			CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO
			CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
			CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
			)
			if (${flag_var} MATCHES "/MD")
				string (REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
			endif ()
		endforeach (flag_var)
	endif (MSVC)
endif()

CMake 3.15 will have CMAKE_MSVC_RUNTIME_LIBRARY option to handle this.

@Chocobo1
Copy link
Contributor

Chocobo1 commented May 1, 2019

@Chocobo1 If I'm not mistaken you can regulate that by passing /Mx flags to CMake in CMAKE_C_FLAGS_RELEASE or CMAKE_C_FLAGS_DEBUG variables. Does it work for you?

Yes, but I'm wishing for an explicit cmake configure option, or even better the snippet evpobr suggested.

CMake 3.15 will have CMAKE_MSVC_RUNTIME_LIBRARY option to handle this.

Glad to know they finally are going to put this nuisance to an end.
However that option still has to set CMP0091 or enable_language() to enable :\ well... better than nothing.

@cristianadam
Copy link

One can use a toolchain with CMake version 3.11+ to change the msvc runtime library without touching the CMake code.

Actually one shouldn't change CMAKE_C|XX_FLAGS in CMake code directly, you should use a toolchain.

See how it can be done here: https://cristianadam.eu/20190223/modifying-the-default-cmake-build-types/

@erikd
Copy link
Member

erikd commented May 2, 2019

Will someone please let me know when this is ready to be merged?

@krokoziabla
Copy link
Contributor Author

Well, I don't have plans to add anything to it. I think it is ready to use in the sense that it provides basic functionality: it can build, test and install the lib. Forsure it always can be improved and extra features can be added but then we will never merge this PR if all of them go here.

I would propose to merge it now and let further extentions of functionality be proposed by other PRs, it there are any

@erikd
Copy link
Member

erikd commented May 2, 2019

@cristianadam @Chocobo1 @evpobr Anything further needed?

@evpobr
Copy link
Contributor

evpobr commented May 2, 2019

@cristianadam @Chocobo1 @evpobr Anything further needed?

No.

@erikd
Copy link
Member

erikd commented May 4, 2019

Thank you all!

This has been merged to master and pushed to https://git.xiph.org/flac.git . It should be mirrored here within a couple of hours.

@erikd erikd closed this May 4, 2019
@krokoziabla krokoziabla deleted the krokoziabla/cmake branch May 6, 2019 09:23
This pull request was closed.
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.

Support CMAKE
8 participants