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

libmagic: add new recipe #19508

Merged
merged 17 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions recipes/libmagic/all/conandata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sources:
"5.45":
url: "https://github.com/file/file/archive/FILE5_45.tar.gz"
valgur marked this conversation as resolved.
Show resolved Hide resolved
sha256: "28c01a5ef1a127ef71758222ca019ba6c6bfa4a8fe20c2b525ce75943ee9da3c"
103 changes: 103 additions & 0 deletions recipes/libmagic/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.apple import fix_apple_shared_install_name
from conan.tools.build import cross_building
from conan.tools.env import VirtualBuildEnv, VirtualRunEnv
from conan.tools.files import copy, get, rm, rmdir, rename
from conan.tools.gnu import Autotools, AutotoolsToolchain
from conan.tools.layout import basic_layout
import os

required_conan_version = ">=1.52.0"

class LibmagicConan(ConanFile):
name = "libmagic"
description = "Magic number recognition library - detect files with data in particular fixed formats."
license = "DocumentRef-COPYING:LicenseRef-BSD-2-Clause-File" # Modified BSD 2-Clause that states no restrictions on US export
Copy link
Contributor

@jwillikers jwillikers Nov 1, 2023

Choose a reason for hiding this comment

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

I'm inclined to think that the DocumenRef- portion should indicate the library or project name instead of the specific file, i.e. DocumentRef-file instead of DocumentRef-COPYING. This fits better with the example in D.3 Simple license expressions. I'm not sure, but I kind of get that feeling this is supposed to be a reference to an SPDX document that we create, i.e. like the SPDXJSONExample-v2.2.spdx.json, but I don't think anyone has done something like that on CCI.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@RubenRBS

Copy link
Member

Choose a reason for hiding this comment

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

homepage = "https://github.com/file/file"
valgur marked this conversation as resolved.
Show resolved Hide resolved
url = "https://github.com/conan-io/conan-center-index"
topics = "magic", "file format"
valgur marked this conversation as resolved.
Show resolved Hide resolved

package_type = "library"
settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
"fPIC": [True, False],
}
default_options = {
"shared": False,
"fPIC": True,
}

def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC

def configure(self):
if self.options.shared:
self.options.rm_safe("fPIC")
self.settings.rm_safe("compiler.libcxx")
self.settings.rm_safe("compiler.cppstd")

def layout(self):
basic_layout(self, src_folder="src")

def requirements(self):
self.requires("bzip2/1.0.8")
self.requires("lzip/1.23")
self.requires("xz_utils/5.4.4")
self.requires("zlib/[>=1.2.11 <2]")
self.requires("zstd/1.5.5")

def validate(self):
if self.settings.os == "Windows":
raise ConanInvalidConfiguration("Windows is not supported yet")
if cross_building(self):
# TODO: Cross-compilation requires a build-context version of 'file' executable to be available.
# It might be possible to build the project for the 'build' platform first to make it work.
raise ConanInvalidConfiguration("Cross-compilation is not supported yet")
valgur marked this conversation as resolved.
Show resolved Hide resolved

def build_requirements(self):
self.tool_requires("libtool/2.4.7")

def source(self):
get(self, **self.conan_data["sources"][self.version], strip_root=True)

def generate(self):
env = VirtualBuildEnv(self)
env.generate()
if not cross_building(self):
env = VirtualRunEnv(self)
env.generate(scope="build")
tc = AutotoolsToolchain(self)
# Set from 'auto' to explicitly enabled
tc.configure_args.append(f"--enable-bzlib={self.dependencies['bzip2'].package_folder}")
tc.configure_args.append(f"--enable-lzlib={self.dependencies['lzip'].package_folder}")
tc.configure_args.append(f"--enable-xzlib={self.dependencies['xz_utils'].package_folder}")
tc.configure_args.append(f"--enable-zlib={self.dependencies['zlib'].package_folder}")
tc.configure_args.append(f"--enable-zstdlib={self.dependencies['zstd'].package_folder}")
tc.generate()

def build(self):
autotools = Autotools(self)
autotools.autoreconf()
autotools.configure()
autotools.make()

def package(self):
copy(self, "COPYING", dst=os.path.join(self.package_folder, "licenses"), src=self.source_folder)
autotools = Autotools(self)
autotools.install()
fix_apple_shared_install_name(self)
rename(self, os.path.join(self.package_folder, "share", "misc"),
os.path.join(self.package_folder, "res"))
rm(self, "*.la", os.path.join(self.package_folder, "lib"))
rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig"))
rmdir(self, os.path.join(self.package_folder, "share"))

def package_info(self):
self.cpp_info.set_property("pkg_config_name", "libmagic")
self.cpp_info.libs = ["magic"]
self.runenv_info.define_path("MAGIC", os.path.join(self.package_folder, "res", "magic.mgc"))
if self.settings.os in ["Linux", "FreeBSD"]:
self.cpp_info.system_libs.append("m")
7 changes: 7 additions & 0 deletions recipes/libmagic/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.15)
project(test_package C)

find_package(libmagic REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.c)
target_link_libraries(${PROJECT_NAME} PRIVATE libmagic::libmagic)
27 changes: 27 additions & 0 deletions recipes/libmagic/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import cmake_layout, CMake
import os


class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "CMakeDeps", "CMakeToolchain", "VirtualRunEnv"
test_type = "explicit"

def requirements(self):
self.requires(self.tested_reference_str, run=True)

def layout(self):
cmake_layout(self)

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if can_run(self):
self.run("file --version", env="conanrun")
bin_path = os.path.join(self.cpp.build.bindir, "test_package")
self.run(bin_path, env="conanrun")
31 changes: 31 additions & 0 deletions recipes/libmagic/all/test_package/test_package.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <magic.h>

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
const char *magic_full;
magic_t magic_cookie;

magic_cookie = magic_open(MAGIC_MIME);

if (magic_cookie == NULL) {
printf("unable to initialize magic library\n");
return 1;
}

char *mgc_path = getenv("MAGIC");
printf("Loading default magic database from %s\n", mgc_path);

if (magic_load(magic_cookie, NULL) != 0) {
printf("cannot load magic database - %s\n", magic_error(magic_cookie));
magic_close(magic_cookie);
return 1;
}

magic_full = magic_file(magic_cookie, argv[0]);
printf("%s\n", magic_full);
magic_close(magic_cookie);
return 0;
}
3 changes: 3 additions & 0 deletions recipes/libmagic/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
versions:
"5.45":
folder: all