Skip to content

Commit

Permalink
Support cx-linux-arm aka running om Mac Docker
Browse files Browse the repository at this point in the history
  • Loading branch information
tsemachh committed Jan 15, 2025
1 parent bc8d330 commit 77bcdde
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
3 changes: 1 addition & 2 deletions src/main/java/com/checkmarx/ast/wrapper/CxThinWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CxThinWrapper {
Expand All @@ -22,7 +21,7 @@ public CxThinWrapper() throws IOException {

public CxThinWrapper(@NonNull Logger logger) throws IOException {
this.logger = logger;
this.executable = Execution.getTempBinary();
this.executable = Execution.getTempBinary(logger);
this.logger.info("Executable path: {} ", executable);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/checkmarx/ast/wrapper/CxWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public CxWrapper(@NonNull CxConfig cxConfig, @NonNull Logger logger) throws IOEx
this.cxConfig = cxConfig;
this.logger = logger;
this.executable = StringUtils.isBlank(this.cxConfig.getPathToExecutable())
? Execution.getTempBinary()
? Execution.getTempBinary(logger)
: this.cxConfig.getPathToExecutable();
this.logger.info("Executable path: {} ", executable);
}
Expand Down
50 changes: 31 additions & 19 deletions src/main/java/com/checkmarx/ast/wrapper/Execution.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.checkmarx.ast.wrapper;

import lombok.NonNull;
import org.slf4j.Logger;

import java.io.*;
Expand All @@ -23,10 +24,10 @@ private Execution() {

}

private static final String OS_NAME = System.getProperty("os.name").toLowerCase(Locale.ENGLISH);
private static final String OS_LINUX = "linux";
private static final String OS_WINDOWS = "windows";
private static final List<String> OS_MAC = Arrays.asList("mac os x", "darwin", "osx");
private static final String OS_MAC = "mac";
private static final List<String> OS_MAC_NAMES = Arrays.asList("mac os x", "darwin", "osx");
private static final String FILE_NAME_LINUX = "cx-linux";
private static final String FILE_NAME_LINUX_ARM = "cx-linux-arm";
private static final String FILE_NAME_MAC = "cx-mac";
Expand Down Expand Up @@ -108,9 +109,9 @@ static String executeCommand(List<String> arguments,
StandardCharsets.UTF_8);
}

static String getTempBinary() throws IOException {
static String getTempBinary(@NonNull Logger logger) throws IOException {
if (executable == null) {
String fileName = detectBinaryName();
String fileName = detectBinaryName(logger);
if (fileName == null) {
throw new IOException("Unsupported architecture");
}
Expand Down Expand Up @@ -143,28 +144,39 @@ private static Process buildProcess(List<String> commands) throws IOException {
return lmBuilder.start();
}

private static String detectBinaryName() {
String osName = OS_NAME;
private static String detectBinaryName(@NonNull Logger logger) {
String osName = System.getProperty("os.name").toLowerCase(Locale.ENGLISH);
String osArch = System.getProperty("os.arch").toLowerCase(Locale.ENGLISH);
String fileName = null;

switch (getOperatingSystemType(osName)) {
case OS_LINUX:
fileName = osArch.contains("arm") || osArch.contains("aarch64") ? FILE_NAME_LINUX_ARM : FILE_NAME_LINUX;
break;
case OS_WINDOWS:
fileName = FILE_NAME_WINDOWS;
break;
case OS_MAC:
fileName = FILE_NAME_MAC;
break;
default:
// Handle unknown OS
logger.error("Unsupported operating system: {} Architecture: {}", osName, osArch);
break;
}
return fileName;
}

private static String getOperatingSystemType(String osName) {
if (osName.contains(OS_LINUX)) {
if (osArch.contains("arm") || osArch.contains("aarch64")) {
fileName = FILE_NAME_LINUX_ARM;
} else {
fileName = FILE_NAME_LINUX;
}
return OS_LINUX;
} else if (osName.contains(OS_WINDOWS)) {
fileName = FILE_NAME_WINDOWS;
return OS_WINDOWS;
} else if (OS_MAC_NAMES.stream().anyMatch(osName::contains)) {
return OS_MAC;
} else {
for (String macStr : OS_MAC) {
if (osName.contains(macStr)) {
fileName = FILE_NAME_MAC;
break;
}
}
return "UNKNOWN"; // Handle unknown OS (optional)
}
return fileName;
}

private static void copyURLToFile(URL source, File destination) throws IOException {
Expand Down

0 comments on commit 77bcdde

Please sign in to comment.