]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Merge pull request #7542 from yuwata/build-cleanup
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 5 Dec 2017 11:13:17 +0000 (12:13 +0100)
committerGitHub <noreply@github.com>
Tue, 5 Dec 2017 11:13:17 +0000 (12:13 +0100)
several build cleanups

.ycm_extra_conf.py
src/analyze/analyze.c
src/basic/fs-util.c
src/mount/mount-tool.c
src/shared/dissect-image.c
src/test/test-fs-util.c

index 4edd3c8a7433ae209b8122b9d734881340b7b7d3..f297deefe2bd0dbe4d115574c72d1be8815ff81c 100644 (file)
-import itertools
+#!/usr/bin/env python
+
+# SPDX-License-Identifier: Unlicense
+#
+# Based on the template file provided by the 'YCM-Generator' project authored by
+# Reuben D'Netto.
+# Jiahui Xie has re-reformatted and expanded the original script in accordance
+# to the requirements of the PEP 8 style guide and 'systemd' project,
+# respectively.
+#
+# The original license is preserved as it is.
+#
+#
+# This is free and unencumbered software released into the public domain.
+#
+# Anyone is free to copy, modify, publish, use, compile, sell, or
+# distribute this software, either in source code form or as a compiled
+# binary, for any purpose, commercial or non-commercial, and by any
+# means.
+#
+# In jurisdictions that recognize copyright laws, the author or authors
+# of this software dedicate any and all copyright interest in the
+# software to the public domain. We make this dedication for the benefit
+# of the public at large and to the detriment of our heirs and
+# successors. We intend this dedication to be an overt act of
+# relinquishment in perpetuity of all present and future rights to this
+# software under copyright law.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+# OTHER DEALINGS IN THE SOFTWARE.
+#
+# For more information, please refer to <http://unlicense.org/>
+
+"""
+YouCompleteMe configuration file tailored to support the 'meson' build system
+used by the 'systemd' project.
+"""
+
+import glob
 import os
-import subprocess
+import ycm_core
 
-def GetFlagsFromMakefile(varname):
-  return subprocess.check_output([
-      "make", "-s", "print-%s" % varname]).decode().split()
 
-
-def Flatten(lists):
-  return list(itertools.chain.from_iterable(lists))
+SOURCE_EXTENSIONS = (".C", ".cpp", ".cxx", ".cc", ".c", ".m", ".mm")
+HEADER_EXTENSIONS = (".H", ".h", ".hxx", ".hpp", ".hh")
 
 
 def DirectoryOfThisScript():
-  return os.path.dirname(os.path.abspath(__file__))
+    """
+    Return the absolute path of the parent directory containing this
+    script.
+    """
+    return os.path.dirname(os.path.abspath(__file__))
+
+
+def GuessBuildDirectory():
+    """
+    Guess the build directory using the following heuristics:
+
+    1. Returns the current directory of this script plus 'build'
+    subdirectory in absolute path if this subdirectory exists.
+
+    2. Otherwise, probes whether there exists any directory
+    containing '.ninja_log' file two levels above the current directory;
+    returns this single directory only if there is one candidate.
+    """
+    result = os.path.join(DirectoryOfThisScript(), "build")
+
+    if os.path.exists(result):
+        return result
+
+    result = glob.glob(os.path.join(DirectoryOfThisScript(),
+                                    "..", "..", "*", ".ninja_log"))
+
+    if not result:
+        return ""
+
+    if 1 != len(result):
+        return ""
+
+    return os.path.split(result[0])[0]
+
+
+def TraverseByDepth(root, include_extensions):
+    """
+    Return a set of child directories of the 'root' containing file
+    extensions specified in 'include_extensions'.
+
+    NOTE:
+        1. The 'root' directory itself is excluded from the result set.
+        2. No subdirectories would be excluded if 'include_extensions' is left
+           to 'None'.
+        3. Each entry in 'include_extensions' must begin with string '.'.
+    """
+    is_root = True
+    result = set()
+    # Perform a depth first top down traverse of the given directory tree.
+    for root_dir, subdirs, file_list in os.walk(root):
+        if not is_root:
+            # print("Relative Root: ", root_dir)
+            # print(subdirs)
+            if include_extensions:
+                get_ext = os.path.splitext
+                subdir_extensions = {
+                    get_ext(f)[-1] for f in file_list if get_ext(f)[-1]
+                }
+                if subdir_extensions & include_extensions:
+                    result.add(root_dir)
+            else:
+                result.add(root_dir)
+        else:
+            is_root = False
+
+    return result
+
+
+_project_src_dir = os.path.join(DirectoryOfThisScript(), "src")
+_include_dirs_set = TraverseByDepth(_project_src_dir, frozenset({".h"}))
+flags = [
+    "-x",
+    "c"
+    # The following flags are partially redundant due to the existence of
+    # 'compile_commands.json'.
+    #    '-Wall',
+    #    '-Wextra',
+    #    '-Wfloat-equal',
+    #    '-Wpointer-arith',
+    #    '-Wshadow',
+    #    '-std=gnu99',
+]
+
+for include_dir in _include_dirs_set:
+    flags.append("-I" + include_dir)
+
+# Set this to the absolute path to the folder (NOT the file!) containing the
+# compile_commands.json file to use that instead of 'flags'. See here for
+# more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html
+#
+# You can get CMake to generate this file for you by adding:
+#   set( CMAKE_EXPORT_COMPILE_COMMANDS 1 )
+# to your CMakeLists.txt file.
+#
+# Most projects will NOT need to set this to anything; you can just change the
+# 'flags' list of compilation flags. Notice that YCM itself uses that approach.
+compilation_database_folder = GuessBuildDirectory()
+
+if os.path.exists(compilation_database_folder):
+    database = ycm_core.CompilationDatabase(compilation_database_folder)
+else:
+    database = None
 
 
 def MakeRelativePathsInFlagsAbsolute(flags, working_directory):
-  if not working_directory:
-    return flags
-  new_flags = []
-  make_next_absolute = False
-  path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]
-  for flag in flags:
-    new_flag = flag
-
-    if make_next_absolute:
-      make_next_absolute = False
-      if not flag.startswith('/'):
-        new_flag = os.path.join(working_directory, flag)
-
-    for path_flag in path_flags:
-      if flag == path_flag:
-        make_next_absolute = True
-        break
-
-      if flag.startswith(path_flag):
-        path = flag[ len(path_flag): ]
-        new_flag = path_flag + os.path.join(working_directory, path)
-        break
-
-    if new_flag:
-      new_flags.append(new_flag)
-  return new_flags
-
-
-def FlagsForFile(filename):
-  relative_to = DirectoryOfThisScript()
-
-  return {
-    'flags': MakeRelativePathsInFlagsAbsolute(flags, relative_to),
-    'do_cache': True
-  }
-
-flags = Flatten(map(GetFlagsFromMakefile, [
-  'AM_CPPFLAGS',
-  'CPPFLAGS',
-  'AM_CFLAGS',
-  'CFLAGS',
-]))
-
-# these flags cause crashes in libclang, so remove them
-flags.remove('-Wlogical-op')
-flags.remove('-Wsuggest-attribute=noreturn')
-flags.remove('-Wdate-time')
-
-# vim: set et ts=2 sw=2:
+    """
+    Iterate through 'flags' and replace the relative paths prefixed by
+    '-isystem', '-I', '-iquote', '--sysroot=' with absolute paths
+    start with 'working_directory'.
+    """
+    if not working_directory:
+        return list(flags)
+    new_flags = []
+    make_next_absolute = False
+    path_flags = ["-isystem", "-I", "-iquote", "--sysroot="]
+    for flag in flags:
+        new_flag = flag
+
+        if make_next_absolute:
+            make_next_absolute = False
+            if not flag.startswith("/"):
+                new_flag = os.path.join(working_directory, flag)
+
+        for path_flag in path_flags:
+            if flag == path_flag:
+                make_next_absolute = True
+                break
+
+            if flag.startswith(path_flag):
+                path = flag[len(path_flag):]
+                new_flag = path_flag + os.path.join(working_directory, path)
+                break
+
+        if new_flag:
+            new_flags.append(new_flag)
+    return new_flags
+
+
+def IsHeaderFile(filename):
+    """
+    Check whether 'filename' is considered as a header file.
+    """
+    extension = os.path.splitext(filename)[1]
+    return extension in HEADER_EXTENSIONS
+
+
+def GetCompilationInfoForFile(filename):
+    """
+    Helper function to look up compilation info of 'filename' in the 'database'.
+    """
+    # The compilation_commands.json file generated by CMake does not have
+    # entries for header files. So we do our best by asking the db for flags for
+    # a corresponding source file, if any. If one exists, the flags for that
+    # file should be good enough.
+    if not database:
+        return None
+
+    if IsHeaderFile(filename):
+        basename = os.path.splitext(filename)[0]
+        for extension in SOURCE_EXTENSIONS:
+            replacement_file = basename + extension
+            if os.path.exists(replacement_file):
+                compilation_info = \
+                    database.GetCompilationInfoForFile(replacement_file)
+                if compilation_info.compiler_flags_:
+                    return compilation_info
+        return None
+    return database.GetCompilationInfoForFile(filename)
+
+
+def FlagsForFile(filename, **kwargs):
+    """
+    Callback function to be invoked by YouCompleteMe in order to get the
+    information necessary to compile 'filename'.
+
+    It returns a dictionary with a single element 'flags'. This element is a
+    list of compiler flags to pass to libclang for the file 'filename'.
+    """
+    if database:
+        # Bear in mind that compilation_info.compiler_flags_ does NOT return a
+        # python list, but a "list-like" StringVec object
+        compilation_info = GetCompilationInfoForFile(filename)
+        if not compilation_info:
+            return None
+
+        final_flags = MakeRelativePathsInFlagsAbsolute(
+            compilation_info.compiler_flags_,
+            compilation_info.compiler_working_dir_)
+
+    else:
+        relative_to = DirectoryOfThisScript()
+        final_flags = MakeRelativePathsInFlagsAbsolute(flags, relative_to)
+
+    return {
+        "flags": final_flags,
+        "do_cache": True
+    }
index 5229b3a082f1735c9d8448013bb118b2bd5c1b7b..8b220d197820566f7e0d539e5326e7b706a42c9c 100644 (file)
@@ -491,11 +491,40 @@ static int pretty_boot_time(sd_bus *bus, char **_buf) {
         size_t size;
         char *ptr;
         int r;
+        usec_t activated_time = USEC_INFINITY;
+        _cleanup_free_ char* path = NULL, *unit_id = NULL;
+        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
 
         r = acquire_boot_times(bus, &t);
         if (r < 0)
                 return r;
 
+        path = unit_dbus_path_from_name(SPECIAL_DEFAULT_TARGET);
+        if (!path)
+                return log_oom();
+
+        r = sd_bus_get_property_string(
+                        bus,
+                        "org.freedesktop.systemd1",
+                        path,
+                        "org.freedesktop.systemd1.Unit",
+                        "Id",
+                        &error,
+                        &unit_id);
+        if (r < 0) {
+                log_error_errno(r, "default.target doesn't seem to exist: %s", bus_error_message(&error, r));
+                unit_id = NULL;
+        }
+
+        r = bus_get_uint64_property(bus, path,
+                        "org.freedesktop.systemd1.Unit",
+                        "ActiveEnterTimestampMonotonic",
+                        &activated_time);
+        if (r < 0) {
+                log_info_errno(r, "default.target seems not to be started. Continuing...");
+                activated_time = USEC_INFINITY;
+        }
+
         ptr = buf;
         size = sizeof(buf);
 
@@ -512,6 +541,9 @@ static int pretty_boot_time(sd_bus *bus, char **_buf) {
         size = strpcpyf(&ptr, size, "%s (userspace) ", format_timespan(ts, sizeof(ts), t->finish_time - t->userspace_time, USEC_PER_MSEC));
         strpcpyf(&ptr, size, "= %s", format_timespan(ts, sizeof(ts), t->firmware_time + t->finish_time, USEC_PER_MSEC));
 
+        if (unit_id && activated_time != USEC_INFINITY)
+                size = strpcpyf(&ptr, size, "\n%s reached after %s in userspace", unit_id, format_timespan(ts, sizeof(ts), activated_time - t->userspace_time, USEC_PER_MSEC));
+
         ptr = strdup(buf);
         if (!ptr)
                 return log_oom();
index 475400177a696449898b384b9d85e1d361a5d146..aa33da48b0d669540090f31397d0ccefabb04f3e 100644 (file)
@@ -621,10 +621,7 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
          * Suggested usage: whenever you want to canonicalize a path, use this function. Pass the absolute path you got
          * as-is: fully qualified and relative to your host's root. Optionally, specify the root parameter to tell this
          * function what to do when encountering a symlink with an absolute path as directory: prefix it by the
-         * specified path.
-         *
-         * Note: there's also chase_symlinks_prefix() (see below), which as first step prefixes the passed path by the
-         * passed root. */
+         * specified path. */
 
         if (original_root) {
                 r = path_make_absolute_cwd(original_root, &root);
@@ -722,6 +719,10 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
                                  * what we got so far. But don't allow this if the remaining path contains "../ or "./"
                                  * or something else weird. */
 
+                                /* If done is "/", as first also contains slash at the head, then remove this redundant slash. */
+                                if (streq_ptr(done, "/"))
+                                        *done = '\0';
+
                                 if (!strextend(&done, first, todo, NULL))
                                         return -ENOMEM;
 
@@ -794,6 +795,10 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
                         done = first;
                         first = NULL;
                 } else {
+                        /* If done is "/", as first also contains slash at the head, then remove this redundant slash. */
+                        if (streq(done, "/"))
+                                *done = '\0';
+
                         if (!strextend(&done, first, NULL))
                                 return -ENOMEM;
                 }
index dd5f62e824c72892aa58a848507f80d7690c2267..da3647e7e2fe50ece1efd1477a21dfe8f5f2d1c0 100644 (file)
@@ -30,6 +30,7 @@
 #include "escape.h"
 #include "fd-util.h"
 #include "fileio.h"
+#include "fs-util.h"
 #include "fstab-util.h"
 #include "mount-util.h"
 #include "pager.h"
@@ -333,19 +334,15 @@ static int parse_argv(int argc, char *argv[]) {
                                 return log_oom();
 
                 } else if (arg_transport == BUS_TRANSPORT_LOCAL) {
-                        _cleanup_free_ char *u = NULL, *p = NULL;
+                        _cleanup_free_ char *u = NULL;
 
                         u = fstab_node_to_udev_node(argv[optind]);
                         if (!u)
                                 return log_oom();
 
-                        r = path_make_absolute_cwd(u, &p);
+                        r = chase_symlinks(u, NULL, 0, &arg_mount_what);
                         if (r < 0)
                                 return log_error_errno(r, "Failed to make path %s absolute: %m", u);
-
-                        arg_mount_what = canonicalize_file_name(p);
-                        if (!arg_mount_what)
-                                return log_error_errno(errno, "Failed to canonicalize path %s: %m", p);
                 } else {
                         arg_mount_what = strdup(argv[optind]);
                         if (!arg_mount_what)
@@ -361,16 +358,9 @@ static int parse_argv(int argc, char *argv[]) {
 
                 if (argc > optind+1) {
                         if (arg_transport == BUS_TRANSPORT_LOCAL) {
-                                _cleanup_free_ char *p = NULL;
-
-                                r = path_make_absolute_cwd(argv[optind+1], &p);
+                                r = chase_symlinks(argv[optind+1], NULL, CHASE_NONEXISTENT, &arg_mount_where);
                                 if (r < 0)
                                         return log_error_errno(r, "Failed to make path %s absolute: %m", argv[optind+1]);
-
-                                arg_mount_where = canonicalize_file_name(p);
-                                if (!arg_mount_where)
-                                        return log_error_errno(errno, "Failed to canonicalize path %s: %m", p);
-
                         } else {
                                 arg_mount_where = strdup(argv[optind+1]);
                                 if (!arg_mount_where)
@@ -829,7 +819,7 @@ static int stop_mount(
 
         r = unit_name_from_path(where, suffix, &mount_unit);
         if (r < 0)
-                return log_error_errno(r, "Failed to make mount unit name from path %s: %m", where);
+                return log_error_errno(r, "Failed to make %s unit name from path %s: %m", suffix + 1, where);
 
         r = sd_bus_message_new_method_call(
                         bus,
@@ -853,8 +843,12 @@ static int stop_mount(
         polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
 
         r = sd_bus_call(bus, m, 0, &error, &reply);
-        if (r < 0)
-                return log_error_errno(r, "Failed to stop mount unit: %s", bus_error_message(&error, r));
+        if (r < 0) {
+                if (streq(suffix, ".automount") &&
+                    sd_bus_error_has_name(&error, "org.freedesktop.systemd1.NoSuchUnit"))
+                        return 0;
+                return log_error_errno(r, "Failed to stop %s unit: %s", suffix + 1, bus_error_message(&error, r));
+        }
 
         if (w) {
                 const char *object;
@@ -991,26 +985,19 @@ static int action_umount(
         }
 
         for (i = optind; i < argc; i++) {
-                _cleanup_free_ char *u = NULL, *a = NULL, *p = NULL;
+                _cleanup_free_ char *u = NULL, *p = NULL;
                 struct stat st;
 
                 u = fstab_node_to_udev_node(argv[i]);
                 if (!u)
                         return log_oom();
 
-                r = path_make_absolute_cwd(u, &a);
+                r = chase_symlinks(u, NULL, 0, &p);
                 if (r < 0) {
                         r2 = log_error_errno(r, "Failed to make path %s absolute: %m", argv[i]);
                         continue;
                 }
 
-                p = canonicalize_file_name(a);
-
-                if (!p) {
-                        r2 = log_error_errno(errno, "Failed to canonicalize path %s: %m", argv[i]);
-                        continue;
-                }
-
                 if (stat(p, &st) < 0)
                         return log_error_errno(errno, "Can't stat %s (from %s): %m", p, argv[i]);
 
index 7835d99020b6d71768408fc64019630b2b17f090..5c69b87c00f46f70fb67c63bd96d1396ce3750af 100644 (file)
@@ -976,8 +976,8 @@ int dissected_image_decrypt(
                 DissectImageFlags flags,
                 DecryptedImage **ret) {
 
-        _cleanup_(decrypted_image_unrefp) DecryptedImage *d = NULL;
 #if HAVE_LIBCRYPTSETUP
+        _cleanup_(decrypted_image_unrefp) DecryptedImage *d = NULL;
         unsigned i;
         int r;
 #endif
index 83ddc398b8834239418245c7db6774753974b309..86d963c4c788124d3c0dc5f664719dde7bdeffca 100644 (file)
@@ -168,6 +168,26 @@ static void test_chase_symlinks(void) {
         assert_se(r > 0 && path_equal(result, "/etc"));
         result = mfree(result);
 
+        r = chase_symlinks("/../.././//../../etc", NULL, 0, &result);
+        assert_se(r > 0);
+        assert_se(streq(result, "/etc"));
+        result = mfree(result);
+
+        r = chase_symlinks("/../.././//../../test-chase.fsldajfl", NULL, CHASE_NONEXISTENT, &result);
+        assert_se(r == 0);
+        assert_se(streq(result, "/test-chase.fsldajfl"));
+        result = mfree(result);
+
+        r = chase_symlinks("/../.././//../../etc", "/", CHASE_PREFIX_ROOT, &result);
+        assert_se(r > 0);
+        assert_se(streq(result, "/etc"));
+        result = mfree(result);
+
+        r = chase_symlinks("/../.././//../../test-chase.fsldajfl", "/", CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &result);
+        assert_se(r == 0);
+        assert_se(streq(result, "/test-chase.fsldajfl"));
+        result = mfree(result);
+
         r = chase_symlinks("/etc/machine-id/foo", NULL, 0, &result);
         assert_se(r == -ENOTDIR);
         result = mfree(result);
@@ -242,6 +262,7 @@ static void test_readlink_and_make_absolute(void) {
         char name2[] = "test-readlink_and_make_absolute/original";
         char name_alias[] = "/tmp/test-readlink_and_make_absolute-alias";
         char *r = NULL;
+        _cleanup_free_ char *pwd = NULL;
 
         assert_se(mkdir_safe(tempdir, 0755, getuid(), getgid(), false) >= 0);
         assert_se(touch(name) >= 0);
@@ -252,6 +273,8 @@ static void test_readlink_and_make_absolute(void) {
         free(r);
         assert_se(unlink(name_alias) >= 0);
 
+        assert_se(pwd = get_current_dir_name());
+
         assert_se(chdir(tempdir) >= 0);
         assert_se(symlink(name2, name_alias) >= 0);
         assert_se(readlink_and_make_absolute(name_alias, &r) >= 0);
@@ -259,6 +282,8 @@ static void test_readlink_and_make_absolute(void) {
         free(r);
         assert_se(unlink(name_alias) >= 0);
 
+        assert_se(chdir(pwd) >= 0);
+
         assert_se(rm_rf(tempdir, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
 }