File: main.c

package info (click to toggle)
igv 2.18.5%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 174,428 kB
  • sloc: java: 121,493; xml: 10,326; sh: 184; ansic: 92; javascript: 54; makefile: 19
file content (146 lines) | stat: -rw-r--r-- 3,827 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//
//  main.c
//  igv-launcher
//
//  Created by James Robinson on 9/14/24.
//
//  Launch IGV on MacOS (https://github.com/igvteam/igv).
//
//  Code adapted from https://incenp.org/notes/2023/universal-java-app-on-macos.html
//

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include <err.h>
#include <mach-o/dyld.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <CoreFoundation/CoreFoundation.h>


/* Remove the last n components of a pathname. The pathname
 * is modified in place. Returns 0 if the requested number
 * of components have been removed, -1 otherwise. */
static int remove_last_component(char *buffer , unsigned n)
{
    char *last_slash = NULL;
    while ( n-- > 0 ) {
        if ( (last_slash = strrchr(buffer, '/')) )
            *last_slash = '\0';
    }
    return last_slash ? 0 : -1;
}

/*
 * Test if a given path exists and if it is a  directory.
 * It returns 1 if given path is directory and  exists
 * otherwise returns 0.
 */
static int isDirectoryExists(const char *path)
{
    struct stat stats;
    stat(path, &stats);

    // Check for file existence
    if (S_ISDIR(stats.st_mode))
        return 1;

    return 0;
}

static int simpleLauncher(int argc, char **argv)
{
    char app_path[PATH_MAX];
    uint32_t path_size = PATH_MAX;
    int ret = 0;

    (void) argc;
    (void) argv;

    /* Get the path to the "Contents" directory. */
    if ( _NSGetExecutablePath(app_path, &path_size) == -1 )
        err(EXIT_FAILURE, "Cannot get application directory");
    if ( remove_last_component(app_path, 2) == -1 )
        errx(EXIT_FAILURE, "Cannot get application directory");

    /* Move to that directory. */
    if ( chdir(app_path) == -1 )
        err(EXIT_FAILURE, "Cannot change current directory");

    char* cmd;
    
    /* See if there is a bundled JDK */
    if(isDirectoryExists("jdk-21")) {
        char jdkPath[2048] = {'\0'};
        snprintf(jdkPath, sizeof(jdkPath), "%s/jdk-21", app_path);
        
        setenv("JAVA_HOME", jdkPath, true);
        
        char javaPath[2048] = {'\0'};
        snprintf(javaPath, sizeof(javaPath), "%s/bin/java", jdkPath);
        cmd = javaPath;
        
        printf("Using bundled JDK");

        //PATH=$JAVA_HOME/bin:$PATH
    } else {
        
        cmd = "java";
        printf("Using System JDK\n");
    }
    
    /* Get user defined extra arguments, if any */
    char extraArgumentsPath[2048] = {'\0'};
    const char *homeDir = getenv("HOME");
    snprintf(extraArgumentsPath, sizeof(extraArgumentsPath), "%s/.igv/java_arguments", homeDir);

    if(access(extraArgumentsPath, F_OK) == 0) {
        char extraArguments[2048] = {'\0'};
        snprintf(extraArguments, sizeof(extraArguments), "@%s", extraArgumentsPath);

        /* Run IGV with user extra arguments */

        
        char* args[] = {
            "java",
            "-showversion",
            "--module-path=Java/lib",
            "-Xmx8g",
            "@Java/igv.args",
            "-Xdock:name=IGV",
            "-Xdock:icon=Resources/IGV_64.png",
            "-Dapple.laf.useScreenMenuBar=true",
            extraArguments,
            "--module=org.igv/org.broad.igv.ui.Main",
            NULL};

        ret = execvp(cmd, args);
        return ret;

    } else {
        
        /* Run IGV without user extra arguments */

        char* args[] = {
            "java",
            "-showversion",
            "--module-path=Java/lib",
            "-Xmx8g",
            "@Java/igv.args",
            "-Xdock:name=IGV",
            "-Xdock:icon=Resources/IGV_64.png",
            "--module=org.igv/org.broad.igv.ui.Main",
            NULL};
        
        ret = execvp(cmd, args);
        return ret;
    }
}

int main(int argc, char **argv)
{
    return simpleLauncher(argc, argv);
}