<commit_before>package com.googlecode.goclipse.builder;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;

import com.googlecode.goclipse.Activator;
import com.googlecode.goclipse.Environment;
import com.googlecode.goclipse.SysUtils;
import com.googlecode.goclipse.preferences.PreferenceConstants;

public class GoLauncher {

	/**
	 * 
	 * @param project name
	 * @param mainfile path in project
	 * @return
	 */
	public static boolean createExecutable(String project, String mainfile) {
		boolean ok = true;
		try {
			MessageConsole console = GoConstants.findConsole("GO");
			MessageConsoleStream con = console.newMessageStream();
			con.println("Creating executable for: " + mainfile + " in project "
					+ project);

			IProject prj = getProject(project, con);
			if (prj != null) {
				
				String binRel = Environment.INSTANCE.getOutputFolder(prj);
				IPath exeBase = prj.getLocation().append(binRel).append(
						GoConstants.EXE_FILE_DIRECTORY);
				IPath objBase = prj.getLocation().append(binRel).append(
						GoConstants.OBJ_FILE_DIRECTORY);
				IPath src = Path.fromOSString(mainfile);
				IResource res = prj.findMember(src);
				String fName = src.removeFileExtension().segment(
						src.segmentCount() - 1);
				String goarch = Activator.getDefault().getPreferenceStore()
						.getString(PreferenceConstants.GOARCH);
				Arch arch = Arch.getArch(goarch);

				IPath objFolder = objBase.append(src.removeFirstSegments(1)
						.removeFileExtension());
				IPath exeFolder = exeBase.append(src.removeFirstSegments(1)
						.removeLastSegments(1));

				String objFileName = objFolder.toOSString()
						+ arch.getExtension();
				String exeFileName = exeFolder.toOSString() + File.separator
						+ fName + arch.getExecutableExt();
				File objFile = new File(objFileName);
				if (!objFile.exists()) {
					con
							.println("file "
									+ mainfile
									+ " was not properly built (there is no obj for it)");
					ok = false;
				}
				File exef = new File(exeFolder.toOSString());
				if (!exef.exists()) {
					exef.mkdirs();
				}
				if (ok) {
					// do linker
					String linkerPath = Activator.getDefault()
							.getPreferenceStore().getString(
									PreferenceConstants.LINKER_PATH);
					ExternalCommand compile = new ExternalCommand(linkerPath);
					compile.setEnvironment(GoConstants.environment());
					compile.setWorkingFolder(exeFolder.toOSString());
					StreamAsLines output = new StreamAsLines();
					compile.setResultsFilter(output);
					List<String> args = new ArrayList<String>();

					compile.setCommand(linkerPath);
					args.clear();
					args.add(GoConstants.COMPILER_OPTION_L);
					args.add(objBase.toOSString());
					args.add(GoConstants.COMPILER_OPTION_O);
					args.add(exeFileName);
					args.add(objFileName);
					con.println("linking ");
					con.println(linkerPath + " " + args);
					String rez = compile.execute(args);
					if (rez != null) {
						con.println("rez:" + rez);
						addMarker(res, rez);
					}
					for (String line : output.getLines()) {
						con.println(line);
						addMarker(res, line);
					}
					con.println("link finished");
					prj.refreshLocal(IResource.DEPTH_INFINITE, null);<commit_msg>linking is done only if necessary<commit_after>package com.googlecode.goclipse.builder;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;

import com.googlecode.goclipse.Activator;
import com.googlecode.goclipse.Environment;
import com.googlecode.goclipse.SysUtils;
import com.googlecode.goclipse.preferences.PreferenceConstants;

public class GoLauncher {

	/**
	 * 
	 * @param project name
	 * @param mainfile path in project
	 * @return
	 */
	public static boolean createExecutable(String project, String mainfile) {
		boolean ok = true;
		try {
			MessageConsole console = GoConstants.findConsole("GO");
			MessageConsoleStream con = console.newMessageStream();

			IProject prj = getProject(project, con);
			if (prj != null) {
				
				String binRel = Environment.INSTANCE.getOutputFolder(prj);
				IPath exeBase = prj.getLocation().append(binRel).append(
						GoConstants.EXE_FILE_DIRECTORY);
				IPath objBase = prj.getLocation().append(binRel).append(
						GoConstants.OBJ_FILE_DIRECTORY);
				IPath src = Path.fromOSString(mainfile);
				IResource res = prj.findMember(src);
				String fName = src.removeFileExtension().segment(
						src.segmentCount() - 1);
				String goarch = Activator.getDefault().getPreferenceStore()
						.getString(PreferenceConstants.GOARCH);
				Arch arch = Arch.getArch(goarch);

				IPath objFolder = objBase.append(src.removeFirstSegments(1)
						.removeFileExtension());
				IPath exeFolder = exeBase.append(src.removeFirstSegments(1)
						.removeLastSegments(1));

				String objFileName = objFolder.toOSString()
						+ arch.getExtension();
				String exeFileName = exeFolder.toOSString() + File.separator
						+ fName + arch.getExecutableExt();
				File objFile = new File(objFileName);
				if (!objFile.exists()) {
					con
							.println("file "
									+ mainfile
									+ " was not properly built (there is no obj for it)");
					ok = false;
				}
				if (ok) {
					File eFile = new File(exeFileName);
					if (eFile.exists() && eFile.lastModified() >= objFile.lastModified()) {
						con.println("executable " + exeFileName + " is up to date");
						ok = false; //exe is up to date
					}
				}
				
				if (ok) {
					con.println("Creating executable for: " + mainfile + " in project "
							+ project);
					
					File exef = new File(exeFolder.toOSString());
					if (!exef.exists()) {
						exef.mkdirs();
					}
					// do linker
					String linkerPath = Activator.getDefault()
							.getPreferenceStore().getString(
									PreferenceConstants.LINKER_PATH);
					ExternalCommand compile = new ExternalCommand(linkerPath);
					compile.setEnvironment(GoConstants.environment());
					compile.setWorkingFolder(exeFolder.toOSString());
					StreamAsLines output = new StreamAsLines();
					compile.setResultsFilter(output);
					List<String> args = new ArrayList<String>();

					compile.setCommand(linkerPath);
					args.clear();
					args.add(GoConstants.COMPILER_OPTION_L);
					args.add(objBase.toOSString());
					args.add(GoConstants.COMPILER_OPTION_O);
					args.add(exeFileName);
					args.add(objFileName);
					con.println("linking ");
					con.println(linkerPath + " " + args);
					String rez = compile.execute(args);
					if (rez != null) {
						con.println("rez:" + rez);
						addMarker(res, rez);
					}
					for (String line : output.getLines()) {
						con.println(line);
						addMarker(res, line);
					}
					con.println("link finished");
					prj.refreshLocal(IResource.DEPTH_INFINITE, null);
{'fraction_non_alphanumeric': 0.120376097983422, 'fraction_numerical': 0.0009897315353210442, 'mean_word_length': 3.6701328711727323, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6815079', 'n_tokens_mistral': 3107, 'n_tokens_neox': 2444, 'n_words': 469}