test/jdk/tools/jlink/plugins/StripNativeDebugSymbolsPlugin/FakeObjCopy.java
author sgehwolf
Thu, 14 Mar 2019 14:04:39 +0100
changeset 54824 adb3a3aa2e52
permissions -rw-r--r--
8214796: Create a jlink plugin for stripping debug info symbols from native libraries Reviewed-by: alanb, mchung, erikj, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
54824
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
     1
/*
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
     2
 * Copyright (c) 2019, Red Hat, Inc.
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
     4
 *
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    10
 *
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    15
 * accompanied this code).
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    16
 *
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    20
 *
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    23
 * questions.
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    24
 */
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    25
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    26
import java.io.File;
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    27
import java.io.PrintWriter;
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    28
import java.nio.file.Files;
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    29
import java.nio.file.Paths;
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    30
import java.nio.file.StandardOpenOption;
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    31
import java.util.Arrays;
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    32
import java.util.List;
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    33
import java.util.stream.Collectors;
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    34
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    35
/**
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    36
 * Fake objcopy used by StripNativeDebugSymbolsTest. It prints the
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    37
 * passed in arguments to a log and creates a fake debug info file
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    38
 * for --only-keep-debug invocation. Note that the first argument is
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    39
 * the path to the log file. This argument will be omitted when
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    40
 * logged.
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    41
 *
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    42
 * Callers need to ensure the log file is properly truncated.
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    43
 *
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    44
 */
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    45
public class FakeObjCopy {
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    46
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    47
    private static final String OBJCOPY_ONLY_KEEP_DEBUG_OPT = "--only-keep-debug";
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    48
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    49
    public static void main(String[] args) throws Exception {
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    50
        if (args.length < 1) {
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    51
            throw new AssertionError("At least one argument expected");
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    52
        }
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    53
        String[] objCopyArgs = new String[args.length - 1];
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    54
        System.arraycopy(args, 1, objCopyArgs, 0, objCopyArgs.length);
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    55
        String logFile = args[0];
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    56
        System.out.println("DEBUG: Fake objcopy called. Log file is: " + logFile);
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    57
        // Log options
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    58
        String line = Arrays.asList(objCopyArgs).stream().collect(Collectors.joining(" "));
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    59
        Files.write(Paths.get(logFile),
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    60
                    List.<String>of(line),
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    61
                    StandardOpenOption.APPEND,
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    62
                    StandardOpenOption.CREATE);
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    63
        // Handle --only-keep-debug option as plugin attempts to read
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    64
        // debug info file after this utility being called.
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    65
        if (objCopyArgs.length == 3 && OBJCOPY_ONLY_KEEP_DEBUG_OPT.equals(objCopyArgs[0])) {
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    66
            handleOnlyKeepDebug(objCopyArgs[2]);
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    67
        }
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    68
    }
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    69
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    70
    private static void handleOnlyKeepDebug(String dbgFile) throws Exception {
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    71
        try (PrintWriter pw = new PrintWriter(new File(dbgFile))) {
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    72
            pw.println("Fake objcopy debug info file");
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    73
        }
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    74
        System.out.println("DEBUG: wrote fake debug file " + dbgFile);
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    75
    }
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    76
adb3a3aa2e52 8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
diff changeset
    77
}