Skip to content

Commit

Permalink
feat: fix libGroup option for Makefile and Gcc
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Callet authored and jspelletier committed Feb 2, 2024
1 parent 9f0ce13 commit 9eed607
Show file tree
Hide file tree
Showing 12 changed files with 294 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Sharpmake/ExtensionMethods.cs
Expand Up @@ -33,7 +33,7 @@ public static bool IsLinkerInvokedViaCompiler(this Platform platform)

public static string GetLinkerOptionPrefix(this Platform platform)
{
if (IsUsingClang(platform) && IsLinkerInvokedViaCompiler(platform))
if(IsLinkerInvokedViaCompiler(platform))
return "-Wl,";

return "";
Expand Down
14 changes: 12 additions & 2 deletions samples/HelloLinux/HelloLinux.Main.sharpmake.cs
Expand Up @@ -2,11 +2,9 @@
// Licensed under the Apache 2.0 License. See LICENSE.md in the project root for license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Sharpmake;
using Sharpmake.Generators.FastBuild;

Expand Down Expand Up @@ -43,6 +41,16 @@ private static void ConfigureAutoCleanup()
Directory.CreateDirectory(Util.FilesAutoCleanupDBPath);
}

public static void OverrideLinuxPlatformDescriptor()
{
// Workaround for regression introduced in Sharpmake@0.20.0.
// Default value for IsLinkerInvokedViaCompiler should be true when generating makefile on Linux.
// See Sharpmake commit e3142832edb3af7f5cc1401365d1e0c1c84fc08e.

var platformDescriptor = (Linux.LinuxPlatform)PlatformRegistry.Get<IPlatformDescriptor>(Platform.linux);
platformDescriptor.IsLinkerInvokedViaCompiler = true;
}

[Sharpmake.Main]
public static void SharpmakeMain(Sharpmake.Arguments arguments)
{
Expand All @@ -56,6 +64,8 @@ public static void SharpmakeMain(Sharpmake.Arguments arguments)
FastBuildSettings.FastBuildMonitor = true;
FastBuildSettings.FastBuildAllowDBMigration = true;

OverrideLinuxPlatformDescriptor();

// for the purpose of this sample, we'll reuse the FastBuild executables that live in the sharpmake source repo
string sharpmakeFastBuildDir = Util.PathGetAbsolute(Globals.RootDirectory, @"..\..\..\tools\FastBuild");
switch (Util.GetExecutingPlatform())
Expand Down
1 change: 1 addition & 0 deletions samples/HelloLinux/codebase/HelloLinux.sharpmake.cs
Expand Up @@ -22,6 +22,7 @@ public override void ConfigureAll(Configuration conf, CommonTarget target)
conf.AddProject<Dll1Project>(target);
conf.AddProject<StaticLib1Project>(target);
conf.AddProject<StaticLib2Project>(target);
conf.AddProject<LibGroupProject>(target);
}
}
}
1 change: 1 addition & 0 deletions samples/HelloLinux/codebase/exe/exe.sharpmake.cs
Expand Up @@ -33,6 +33,7 @@ public override void ConfigureAll(Configuration conf, CommonTarget target)
conf.AddPrivateDependency<StaticLib2Project>(target);
conf.AddPrivateDependency<HeaderOnlyLibProject>(target);
conf.AddPrivateDependency<ExternalLibProject>(target);
conf.AddPrivateDependency<LibGroupProject>(target);

conf.Defines.Add("CREATION_DATE=\"October 2020\"");
}
Expand Down
47 changes: 47 additions & 0 deletions samples/HelloLinux/codebase/lib_group/lib_group.sharpmake.cs
@@ -0,0 +1,47 @@
// Copyright (c) Ubisoft. All Rights Reserved.
// Licensed under the Apache 2.0 License. See LICENSE.md in the project root for license information.

using Sharpmake;

namespace HelloLinux
{
[Sharpmake.Generate]
public class LibGroupProject : CommonProject
{
public LibGroupProject()
{
AddTargets(CommonTarget.GetDefaultTargets());
Name = "lib_group";
}

public override void ConfigureAll(Configuration conf, CommonTarget target)
{
base.ConfigureAll(conf, target);
ConfigureLibraries(conf, target);

conf.Output = Configuration.OutputType.Dll;
conf.IncludePaths.Add(SourceRootPath);
conf.SolutionFolder = "SharedLibs";

conf.PrecompHeader = "precomp.h";
conf.PrecompSource = "precomp.cpp";

conf.AdditionalCompilerOptions.Add("-fPIC");

conf.Defines.Add("UTIL_DLL_EXPORT");
conf.ExportDefines.Add("UTIL_DLL_IMPORT");
}

public void ConfigureLibraries(Configuration conf, CommonTarget target) {

// Enable library group with Clang and GCC to allow libraries that have circular dependencies.
conf.Options.Add(Options.Makefile.Linker.LibGroup.Enable);

conf.LibraryFiles.Add("m");
conf.AddPrivateDependency<StaticLib1Project>(target);
conf.AddPrivateDependency<Dll1Project>(target);
conf.AddPrivateDependency<ExternalLibProject>(target);

}
}
}
2 changes: 2 additions & 0 deletions samples/HelloLinux/codebase/lib_group/precomp.cpp
@@ -0,0 +1,2 @@
#include "precomp.h"

3 changes: 3 additions & 0 deletions samples/HelloLinux/codebase/lib_group/precomp.h
@@ -0,0 +1,3 @@
#pragma once

#include <vector>
36 changes: 36 additions & 0 deletions samples/HelloLinux/codebase/lib_group/util_dll.cpp
@@ -0,0 +1,36 @@
#include "precomp.h"
#include "util_dll.h"

#include <cmath>
#include <iostream>
#include "src/util_static_lib1.h"

int UtilDll1::ComputeMagicNumber(const std::vector<int>& someInts)
{
int acc = 0;
for (int item : someInts)
acc += item;

#if _DEBUG
std::cout << "- Dll1 is built in Debug"
# if USES_FASTBUILD
" with FastBuild"
# endif
"!" << std::endl;
#endif

#if NDEBUG
std::cout << "- Dll1 is built in Release"
# if USES_FASTBUILD
" with FastBuild"
# endif
"!" << std::endl;
#endif

acc += static_lib1_utils::GetRandomPosition();

acc += cosf(M_PI);

return acc;
}

18 changes: 18 additions & 0 deletions samples/HelloLinux/codebase/lib_group/util_dll.h
@@ -0,0 +1,18 @@
#pragma once
#include <vector>

#if defined(UTIL_DLL_EXPORT) && HAVE_VISIBILITY
# define UTIL_DLL __attribute__((__visibility("default")))
#elif defined(UTIL_DLL_EXPORT) && defined(_MSC_VER)
# define UTIL_DLL __declspec(dllexport)
#elif defined(UTIL_DLL_IMPORT) && defined(_MSC_VER)
#define UTIL_DLL __declspec(dllimport)
#else
#define UTIL_DLL
#endif

struct UtilDll1
{
UTIL_DLL int ComputeMagicNumber(const std::vector<int>& someInts);
};

Expand Up @@ -14,14 +14,14 @@ ifeq ($(config),debug)
TARGETDIR = ../../bin/linux_debug
TARGET = $(TARGETDIR)/exe
DEFINES += -D "CREATION_DATE=\"October 2020\"" -D "UTIL_DLL_IMPORT" -D "_DEBUG"
INCLUDES += -I../../../dll1 -I../../../header-only-lib -I../../../static\ lib2
INCLUDES += -I../../../dll1 -I../../../header-only-lib -I../../../lib_group -I../../../static\ lib2
CPPFLAGS += -MMD -MP $(DEFINES) $(INCLUDES)
CFLAGS += $(CPPFLAGS) -g -Wall
CXXFLAGS += $(CFLAGS) -fno-exceptions -fno-rtti
LDFLAGS += -L../../bin/linux_debug -L../../lib/linux_debug/curl -L../../lib/linux_debug/static\ lib2 -Wl,-rpath='$$ORIGIN'
LDLIBS += -l:libcurl.a -l:libdll1.so -l:libstatic\ lib2.a -l:libuuid.so
LDLIBS += -l:libcurl.a -l:libdll1.so -l:liblib_group.so -l:libstatic\ lib2.a -l:libuuid.so
RESFLAGS += $(DEFINES) $(INCLUDES)
LDDEPS += ../../bin/linux_debug/libdll1.so ../../lib/linux_debug/static\ lib2/libstatic\ lib2.a ../../lib/linux_debug/static_lib1/libstatic_lib1.a
LDDEPS += ../../bin/linux_debug/libdll1.so ../../bin/linux_debug/liblib_group.so ../../lib/linux_debug/static\ lib2/libstatic\ lib2.a ../../lib/linux_debug/static_lib1/libstatic_lib1.a
LINKCMD = $(CXX) -o $(TARGET) $(OBJECTS) $(LDFLAGS) $(RESOURCES) $(LDLIBS)
PCH = ../../../exe/stdafx.h
PCHOUT = $(OBJDIR)/stdafx.h
Expand All @@ -47,14 +47,14 @@ ifeq ($(config),release)
TARGETDIR = ../../bin/linux_release
TARGET = $(TARGETDIR)/exe
DEFINES += -D "CREATION_DATE=\"October 2020\"" -D "NDEBUG" -D "UTIL_DLL_IMPORT"
INCLUDES += -I../../../dll1 -I../../../header-only-lib -I../../../static\ lib2
INCLUDES += -I../../../dll1 -I../../../header-only-lib -I../../../lib_group -I../../../static\ lib2
CPPFLAGS += -MMD -MP $(DEFINES) $(INCLUDES)
CFLAGS += $(CPPFLAGS) -g -O3 -Wall
CXXFLAGS += $(CFLAGS) -fno-exceptions -fno-rtti
LDFLAGS += -L../../bin/linux_release -L../../lib/linux_release/curl -L../../lib/linux_release/static\ lib2 -Wl,-rpath='$$ORIGIN'
LDLIBS += -l:libcurl.a -l:libdll1.so -l:libstatic\ lib2.a -l:libuuid.so
LDLIBS += -l:libcurl.a -l:libdll1.so -l:liblib_group.so -l:libstatic\ lib2.a -l:libuuid.so
RESFLAGS += $(DEFINES) $(INCLUDES)
LDDEPS += ../../bin/linux_release/libdll1.so ../../lib/linux_release/static\ lib2/libstatic\ lib2.a ../../lib/linux_release/static_lib1/libstatic_lib1.a
LDDEPS += ../../bin/linux_release/libdll1.so ../../bin/linux_release/liblib_group.so ../../lib/linux_release/static\ lib2/libstatic\ lib2.a ../../lib/linux_release/static_lib1/libstatic_lib1.a
LINKCMD = $(CXX) -o $(TARGET) $(OBJECTS) $(LDFLAGS) $(RESOURCES) $(LDLIBS)
PCH = ../../../exe/stdafx.h
PCHOUT = $(OBJDIR)/stdafx.h
Expand Down
@@ -0,0 +1,159 @@
# Generated by Sharpmake -- Do not edit !
ifndef config
config=debug
endif

ifndef verbose
SILENT = @
endif

ifeq ($(config),debug)
CXX = g++
AR = ar
OBJDIR = ../../obj/linux_debug/lib_group
TARGETDIR = ../../bin/linux_debug
TARGET = $(TARGETDIR)/liblib_group.so
DEFINES += -D "UTIL_DLL_EXPORT" -D "UTIL_DLL_IMPORT" -D "_DEBUG"
INCLUDES += -I../../../dll1 -I../../../lib_group -I../../../static_lib1
CPPFLAGS += -MMD -MP $(DEFINES) $(INCLUDES)
CFLAGS += $(CPPFLAGS) -g -Wall -fPIC
CXXFLAGS += $(CFLAGS) -fno-exceptions -fno-rtti
LDFLAGS += -L../../bin/linux_debug -L../../lib/linux_debug/curl -L../../lib/linux_debug/static_lib1
LDLIBS += -Wl,--start-group -l:libcurl.a -l:libdll1.so -l:libstatic_lib1.a -l:libm.a -Wl,--end-group
RESFLAGS += $(DEFINES) $(INCLUDES)
LDDEPS += ../../bin/linux_debug/libdll1.so ../../lib/linux_debug/static_lib1/libstatic_lib1.a
LINKCMD = $(CXX) -shared -o $(TARGET) $(OBJECTS) $(LDFLAGS) $(RESOURCES) $(LDLIBS)
PCH = ../../../lib_group/precomp.h
PCHOUT = $(OBJDIR)/precomp.h
GCH = $(OBJDIR)/precomp.h.gch
PCHCMD = -include $(PCHOUT)
define PREBUILDCMDS
mkdir -p $(TARGETDIR)/../../package
endef
define PRELINKCMDS

endef
define POSTBUILDCMDS
cp $(TARGET) $(TARGETDIR)/../../package
endef
define POSTFILECMDS
endef
endif

ifeq ($(config),release)
CXX = g++
AR = ar
OBJDIR = ../../obj/linux_release/lib_group
TARGETDIR = ../../bin/linux_release
TARGET = $(TARGETDIR)/liblib_group.so
DEFINES += -D "NDEBUG" -D "UTIL_DLL_EXPORT" -D "UTIL_DLL_IMPORT"
INCLUDES += -I../../../dll1 -I../../../lib_group -I../../../static_lib1
CPPFLAGS += -MMD -MP $(DEFINES) $(INCLUDES)
CFLAGS += $(CPPFLAGS) -g -O3 -Wall -fPIC
CXXFLAGS += $(CFLAGS) -fno-exceptions -fno-rtti
LDFLAGS += -L../../bin/linux_release -L../../lib/linux_release/curl -L../../lib/linux_release/static_lib1
LDLIBS += -Wl,--start-group -l:libcurl.a -l:libdll1.so -l:libstatic_lib1.a -l:libm.a -Wl,--end-group
RESFLAGS += $(DEFINES) $(INCLUDES)
LDDEPS += ../../bin/linux_release/libdll1.so ../../lib/linux_release/static_lib1/libstatic_lib1.a
LINKCMD = $(CXX) -shared -o $(TARGET) $(OBJECTS) $(LDFLAGS) $(RESOURCES) $(LDLIBS)
PCH = ../../../lib_group/precomp.h
PCHOUT = $(OBJDIR)/precomp.h
GCH = $(OBJDIR)/precomp.h.gch
PCHCMD = -include $(PCHOUT)
define PREBUILDCMDS
mkdir -p $(TARGETDIR)/../../package
endef
define PRELINKCMDS

endef
define POSTBUILDCMDS
cp $(TARGET) $(TARGETDIR)/../../package
endef
define POSTFILECMDS
endef
endif

ifeq ($(config),debug)
OBJECTS += $(OBJDIR)/precomp.o
OBJECTS += $(OBJDIR)/util_dll.o
endif

ifeq ($(config),release)
OBJECTS += $(OBJDIR)/precomp.o
OBJECTS += $(OBJDIR)/util_dll.o
endif

RESOURCES := \

SHELLTYPE := msdos
ifeq (,$(ComSpec)$(COMSPEC))
SHELLTYPE := posix
endif
ifeq (/bin,$(findstring /bin,$(SHELL)))
SHELLTYPE := posix
endif

.PHONY: clean prebuild prelink

all: $(TARGETDIR) $(OBJDIR) prebuild prelink $(TARGET)
@:

$(TARGET): $(GCH) $(OBJECTS) $(LDDEPS) $(RESOURCES) | $(TARGETDIR)
@echo Linking lib_group
$(SILENT) $(LINKCMD)
$(POSTBUILDCMDS)

$(TARGETDIR):
@echo Creating $(TARGETDIR)
ifeq (posix,$(SHELLTYPE))
$(SILENT) mkdir -p $(TARGETDIR)
else
$(SILENT) if not exist $(subst /,\\,$(TARGETDIR)) mkdir $(subst /,\\,$(TARGETDIR))
endif

ifneq ($(OBJDIR),$(TARGETDIR))
$(OBJDIR):
@echo Creating $(OBJDIR)
ifeq (posix,$(SHELLTYPE))
$(SILENT) mkdir -p $(OBJDIR)
else
$(SILENT) if not exist $(subst /,\\,$(OBJDIR)) mkdir $(subst /,\\,$(OBJDIR))
endif
endif

clean:
@echo Cleaning lib_group
ifeq (posix,$(SHELLTYPE))
$(SILENT) rm -f $(TARGET)
$(SILENT) rm -rf $(OBJDIR)
else
$(SILENT) if exist $(subst /,\\,$(TARGET)) del $(subst /,\\,$(TARGET))
$(SILENT) if exist $(subst /,\\,$(OBJDIR)) rmdir /s /q $(subst /,\\,$(OBJDIR))
endif

prebuild:
$(PREBUILDCMDS)

prelink:
$(PRELINKCMDS)

ifneq (,$(PCH))
$(GCH): $(PCH) | $(OBJDIR)
@echo $(notdir $<)
-$(SILENT) cp $< $(OBJDIR)
$(SILENT) $(CXX) $(CXXFLAGS) -xc++-header -o "$@" -c "$<"
$(SILENT) $(POSTFILECMDS)
endif

$(OBJDIR)/precomp.o: ../../../lib_group/precomp.cpp $(GCH) | $(OBJDIR)
@echo $(notdir $<)
$(SILENT) $(CXX) $(CXXFLAGS) $(PCHCMD) -o "$@" -c "$<"
$(SILENT) $(POSTFILECMDS)

$(OBJDIR)/util_dll.o: ../../../lib_group/util_dll.cpp $(GCH) | $(OBJDIR)
@echo $(notdir $<)
$(SILENT) $(CXX) $(CXXFLAGS) $(PCHCMD) -o "$@" -c "$<"
$(SILENT) $(POSTFILECMDS)

-include $(OBJECTS:%.o=%.d)
-include $(GCH:%.gch=%.d)

0 comments on commit 9eed607

Please sign in to comment.