1cf200d32Sopenharmony_ci# Makefile for GPT fdisk
2cf200d32Sopenharmony_ci
3cf200d32Sopenharmony_ci# Copyright (c) 2022 by Rod Smith
4cf200d32Sopenharmony_ci# This program is licensed under the terms of the GNU GPL, version 2,
5cf200d32Sopenharmony_ci# or (at your option) any later version.
6cf200d32Sopenharmony_ci# You should have received a copy of the GNU General Public License
7cf200d32Sopenharmony_ci# along with this program. If not, see <http://www.gnu.org/licenses/>.
8cf200d32Sopenharmony_ci
9cf200d32Sopenharmony_ci# This is a consolidated Makefile for Linux, FreeBSD, Solaris (untested),
10cf200d32Sopenharmony_ci# macOS, and Windows (x86_64 and i686).
11cf200d32Sopenharmony_ci
12cf200d32Sopenharmony_ci# Builds for host OS by default; pass TARGET={target_os} to cross-compile,
13cf200d32Sopenharmony_ci# where {target_os} is one of linux, freebsd, solaris, macos, win32, or win64.
14cf200d32Sopenharmony_ci# Appropriate cross-compiler support must be installed, of course, and build
15cf200d32Sopenharmony_ci# options below may need to be changed.
16cf200d32Sopenharmony_ci
17cf200d32Sopenharmony_ci# DETECTED_OS is used both to set certain options for the build
18cf200d32Sopenharmony_ci# environment and to determine the default TARGET if it's not
19cf200d32Sopenharmony_ci# otherwise specified.
20cf200d32Sopenharmony_ciDETECTED_OS := $(shell uname -s)
21cf200d32Sopenharmony_ci
22cf200d32Sopenharmony_ciifeq ($(origin TARGET),undefined)
23cf200d32Sopenharmony_ci  $(info TARGET is not set; trying to determine target based on host OS....)
24cf200d32Sopenharmony_ci  $(info Detected OS is $(DETECTED_OS))
25cf200d32Sopenharmony_ci  ifeq ($(DETECTED_OS),Linux)
26cf200d32Sopenharmony_ci    # Note: TARGET is set to "linux", but this is never tested, since it's
27cf200d32Sopenharmony_ci    # the default.
28cf200d32Sopenharmony_ci    TARGET=linux
29cf200d32Sopenharmony_ci  else ifeq ($(DETECTED_OS),Darwin)
30cf200d32Sopenharmony_ci    TARGET=macos
31cf200d32Sopenharmony_ci  else ifeq ($(DETECTED_OS),MINGW64_NT-10.0-19042)
32cf200d32Sopenharmony_ci    # Works for my MSYS2 installation, but seems awfully version-specific
33cf200d32Sopenharmony_ci    # Also, uname may not exist in some Windows environments.
34cf200d32Sopenharmony_ci    TARGET=windows
35cf200d32Sopenharmony_ci  else ifeq ($(DETECTED_OS),FreeBSD)
36cf200d32Sopenharmony_ci    TARGET=freebsd
37cf200d32Sopenharmony_ci  else ifeq ($(DETECTED_OS),SunOS)
38cf200d32Sopenharmony_ci    TARGET=solaris
39cf200d32Sopenharmony_ci  endif
40cf200d32Sopenharmony_ciendif
41cf200d32Sopenharmony_ci
42cf200d32Sopenharmony_ci# A second way to detect Windows....
43cf200d32Sopenharmony_ciifeq ($(origin TARGET),undefined)
44cf200d32Sopenharmony_ci  ifeq ($(OS),Windows_NT)
45cf200d32Sopenharmony_ci    TARGET=windows
46cf200d32Sopenharmony_ci  endif
47cf200d32Sopenharmony_ciendif
48cf200d32Sopenharmony_ci
49cf200d32Sopenharmony_ci# For Windows, we need to know the bit depth, too
50cf200d32Sopenharmony_ciifeq ($(TARGET),windows)
51cf200d32Sopenharmony_ci  ARCH=$(shell uname -m)
52cf200d32Sopenharmony_ci  $(info ARCH is $(ARCH))
53cf200d32Sopenharmony_ci  ifeq ($(ARCH),x86_64)
54cf200d32Sopenharmony_ci    TARGET=win64
55cf200d32Sopenharmony_ci  else ifeq ($(ARCH),i686)
56cf200d32Sopenharmony_ci    TARGET=win32
57cf200d32Sopenharmony_ci  else
58cf200d32Sopenharmony_ci    # In theory, there could be ARM versions, but we aren't set up for them yet;
59cf200d32Sopenharmony_ci    # also, default to win64 in case uname doesn't exist on the system
60cf200d32Sopenharmony_ci    TARGET=win64
61cf200d32Sopenharmony_ci  endif
62cf200d32Sopenharmony_ciendif
63cf200d32Sopenharmony_ci
64cf200d32Sopenharmony_ci$(info Build target is $(TARGET))
65cf200d32Sopenharmony_ci
66cf200d32Sopenharmony_ci# Default/Linux settings....
67cf200d32Sopenharmony_ciSTRIP?=strip
68cf200d32Sopenharmony_ci#CXXFLAGS+=-O2 -Wall -D_FILE_OFFSET_BITS=64 -D USE_UTF16
69cf200d32Sopenharmony_ciCXXFLAGS+=-O2 -Wall -D_FILE_OFFSET_BITS=64
70cf200d32Sopenharmony_ciLDFLAGS+=
71cf200d32Sopenharmony_ciLDLIBS+=-luuid #-licuio -licuuc
72cf200d32Sopenharmony_ciFATBINFLAGS=
73cf200d32Sopenharmony_ciTHINBINFLAGS=
74cf200d32Sopenharmony_ciSGDISK_LDLIBS=-lpopt
75cf200d32Sopenharmony_ciCGDISK_LDLIBS=-lncursesw
76cf200d32Sopenharmony_ciLIB_NAMES=crc32 support guid gptpart mbrpart basicmbr mbr gpt bsd parttypes attributes diskio diskio-unix
77cf200d32Sopenharmony_ciMBR_LIBS=support diskio diskio-unix basicmbr mbrpart
78cf200d32Sopenharmony_ciALL=gdisk cgdisk sgdisk fixparts
79cf200d32Sopenharmony_ciFN_EXTENSION=
80cf200d32Sopenharmony_ci
81cf200d32Sopenharmony_ci# Settings for non-Linux OSes....
82cf200d32Sopenharmony_ciifeq ($(TARGET),win64)
83cf200d32Sopenharmony_ci  CXX=x86_64-w64-mingw32-g++
84cf200d32Sopenharmony_ci  ifeq ($(DETECTED_OS),Linux)
85cf200d32Sopenharmony_ci    STRIP=x86_64-w64-mingw32-strip
86cf200d32Sopenharmony_ci  else
87cf200d32Sopenharmony_ci    STRIP=strip
88cf200d32Sopenharmony_ci  endif
89cf200d32Sopenharmony_ci  CXXFLAGS=-O2 -Wall -D_FILE_OFFSET_BITS=64 -static -static-libgcc -static-libstdc++
90cf200d32Sopenharmony_ci  #CXXFLAGS=-O2 -Wall -D_FILE_OFFSET_BITS=64 -I /usr/local/include -I/opt/local/include -g
91cf200d32Sopenharmony_ci  LDFLAGS+=-static -static-libgcc -static-libstdc++
92cf200d32Sopenharmony_ci  LDLIBS+=-lrpcrt4
93cf200d32Sopenharmony_ci  SGDISK_LDLIBS=-lpopt -lintl -liconv
94cf200d32Sopenharmony_ci  LIB_NAMES=guid gptpart bsd parttypes attributes crc32 mbrpart basicmbr mbr gpt support diskio diskio-windows
95cf200d32Sopenharmony_ci  MBR_LIBS=support diskio diskio-windows basicmbr mbrpart
96cf200d32Sopenharmony_ci  FN_EXTENSION=64.exe
97cf200d32Sopenharmony_ci  ifeq ($(DETECTED_OS),Linux)
98cf200d32Sopenharmony_ci    # Omit cgdisk when building under Linux for Windows because it doesn't
99cf200d32Sopenharmony_ci    # work on my system
100cf200d32Sopenharmony_ci    ALL=gdisk sgdisk fixparts
101cf200d32Sopenharmony_ci  endif
102cf200d32Sopenharmony_cielse ifeq ($(TARGET),win32)
103cf200d32Sopenharmony_ci  CXX=i686-w64-mingw32-g++
104cf200d32Sopenharmony_ci  ifeq ($(DETECTED_OS),Linux)
105cf200d32Sopenharmony_ci    STRIP=i686-w64-mingw32-strip
106cf200d32Sopenharmony_ci  else
107cf200d32Sopenharmony_ci    STRIP=strip
108cf200d32Sopenharmony_ci  endif
109cf200d32Sopenharmony_ci  CXXFLAGS=-O2 -Wall -D_FILE_OFFSET_BITS=64 -static -static-libgcc -static-libstdc++
110cf200d32Sopenharmony_ci  #CXXFLAGS=-O2 -Wall -D_FILE_OFFSET_BITS=64 -I /usr/local/include -I/opt/local/include
111cf200d32Sopenharmony_ci  LDFLAGS+=-static -static-libgcc -static-libstdc++
112cf200d32Sopenharmony_ci  LDLIBS+=-lrpcrt4
113cf200d32Sopenharmony_ci  SGDISK_LDLIBS=-lpopt -lintl -liconv
114cf200d32Sopenharmony_ci  LIB_NAMES=guid gptpart bsd parttypes attributes crc32 mbrpart basicmbr mbr gpt support diskio diskio-windows
115cf200d32Sopenharmony_ci  MBR_LIBS=support diskio diskio-windows basicmbr mbrpart
116cf200d32Sopenharmony_ci  FN_EXTENSION=32.exe
117cf200d32Sopenharmony_ci  ifeq ($(DETECTED_OS),Linux)
118cf200d32Sopenharmony_ci    # Omit cgdisk when building for Windows under Linux because it doesn't
119cf200d32Sopenharmony_ci    # work on my system
120cf200d32Sopenharmony_ci    ALL=gdisk sgdisk fixparts
121cf200d32Sopenharmony_ci  endif
122cf200d32Sopenharmony_cielse ifeq ($(TARGET),freebsd)
123cf200d32Sopenharmony_ci  CXX=clang++
124cf200d32Sopenharmony_ci  CXXFLAGS+=-O2 -Wall -D_FILE_OFFSET_BITS=64 -I /usr/local/include
125cf200d32Sopenharmony_ci  LDFLAGS+=-L/usr/local/lib
126cf200d32Sopenharmony_ci  LDLIBS+=-luuid #-licuio
127cf200d32Sopenharmony_cielse ifeq ($(TARGET),macos)
128cf200d32Sopenharmony_ci  FATBINFLAGS=-arch x86_64 -arch arm64 -mmacosx-version-min=10.9
129cf200d32Sopenharmony_ci  THINBINFLAGS=-arch x86_64 -mmacosx-version-min=10.9
130cf200d32Sopenharmony_ci  CXXFLAGS=$(FATBINFLAGS) -O2 -Wall -D_FILE_OFFSET_BITS=64 -stdlib=libc++ -I/opt/local/include -I /usr/local/include -I/opt/local/include
131cf200d32Sopenharmony_ci  LDLIBS= #-licucore
132cf200d32Sopenharmony_ci  CGDISK_LDLIBS=/usr/local/Cellar/ncurses/6.2/lib/libncurses.dylib
133cf200d32Sopenharmony_cielse ifeq ($(TARGET),solaris)
134cf200d32Sopenharmony_ci  CXXFLAGS+=-Wall -D_FILE_OFFSET_BITS=64 -I/usr/include/ncurses
135cf200d32Sopenharmony_ci  LDFLAGS+=-L/lib -licuio -licuuc -luuid
136cf200d32Sopenharmony_ciendif
137cf200d32Sopenharmony_ci
138cf200d32Sopenharmony_ci# More default settings, for all OSes....
139cf200d32Sopenharmony_ciLIB_OBJS=$(LIB_NAMES:=.o)
140cf200d32Sopenharmony_ciMBR_LIB_OBJS=$(MBR_LIBS:=.o)
141cf200d32Sopenharmony_ciLIB_HEADERS=$(LIB_NAMES:=.h)
142cf200d32Sopenharmony_ciDEPEND= makedepend $(CXXFLAGS)
143cf200d32Sopenharmony_ciALL_EXE=$(ALL:=$(FN_EXTENSION))
144cf200d32Sopenharmony_ci
145cf200d32Sopenharmony_ciall:	$(ALL)
146cf200d32Sopenharmony_ci
147cf200d32Sopenharmony_cigdisk:	$(LIB_OBJS) gdisk.o gpttext.o
148cf200d32Sopenharmony_ci	$(CXX) $(LIB_OBJS) gdisk.o gpttext.o $(LDFLAGS) $(LDLIBS) $(FATBINFLAGS) -o gdisk$(FN_EXTENSION)
149cf200d32Sopenharmony_ci
150cf200d32Sopenharmony_cicgdisk: $(LIB_OBJS) cgdisk.o gptcurses.o
151cf200d32Sopenharmony_ci	$(CXX) $(LIB_OBJS) cgdisk.o gptcurses.o $(LDFLAGS) $(LDLIBS) $(CGDISK_LDLIBS) -o cgdisk$(FN_EXTENSION)
152cf200d32Sopenharmony_ci
153cf200d32Sopenharmony_cisgdisk: $(LIB_OBJS) sgdisk.o gptcl.o
154cf200d32Sopenharmony_ci	$(CXX) $(LIB_OBJS) sgdisk.o gptcl.o $(LDFLAGS) $(LDLIBS) $(SGDISK_LDLIBS) $(THINBINFLAGS) -o sgdisk$(FN_EXTENSION)
155cf200d32Sopenharmony_ci
156cf200d32Sopenharmony_cifixparts: $(MBR_LIB_OBJS) fixparts.o
157cf200d32Sopenharmony_ci	$(CXX) $(MBR_LIB_OBJS) fixparts.o $(LDFLAGS) $(FATBINFLAGS) -o fixparts$(FN_EXTENSION)
158cf200d32Sopenharmony_ci
159cf200d32Sopenharmony_citest:
160cf200d32Sopenharmony_ci	./gdisk_test.sh
161cf200d32Sopenharmony_ci
162cf200d32Sopenharmony_cilint:	#no pre-reqs
163cf200d32Sopenharmony_ci	lint $(SRCS)
164cf200d32Sopenharmony_ci
165cf200d32Sopenharmony_ciclean:	#no pre-reqs
166cf200d32Sopenharmony_ci	rm -f core *.o *~ $(ALL_EXE)
167cf200d32Sopenharmony_ci
168cf200d32Sopenharmony_cistrip:	#no pre-reqs
169cf200d32Sopenharmony_ci	$(STRIP) $(ALL_EXE)
170cf200d32Sopenharmony_ci
171cf200d32Sopenharmony_ci# what are the source dependencies
172cf200d32Sopenharmony_cidepend: $(SRCS)
173cf200d32Sopenharmony_ci	$(DEPEND) $(SRCS)
174cf200d32Sopenharmony_ci
175cf200d32Sopenharmony_ci$(OBJS):
176cf200d32Sopenharmony_ci	$(CRITICAL_CXX_FLAGS) 
177cf200d32Sopenharmony_ci
178cf200d32Sopenharmony_ci# makedepend dependencies below -- type "makedepend *.cc" to regenerate....
179cf200d32Sopenharmony_ci# DO NOT DELETE
180