From: Guillem Jover <guillem@debian.org>
Date: Fri, 24 Jan 2025 02:18:14 +0100
X-Dgit-Generated: 4.11+dfsg1-1.1 396fb9b55ee4e86741608cd4bddfdab41168a7a2
Subject: Enable portability beyond i386 and amd64

Closes: #646778

---

diff --git a/app/Makefile b/app/Makefile
index 9d16c0f..3a3275c 100644
--- a/app/Makefile
+++ b/app/Makefile
@@ -1,41 +1,8 @@
 C++ = g++
 
-ifndef os
-   os = LINUX
-endif
-
-ifndef arch
-   arch = IA32
-endif
-
-CCFLAGS = -Wall -D$(os) -I../src -finline-functions -O3
-
-ifeq ($(arch), IA32)
-   CCFLAGS += -DIA32 #-mcpu=pentiumpro -march=pentiumpro -mmmx -msse
-endif
-
-ifeq ($(arch), POWERPC)
-   CCFLAGS += -mcpu=powerpc
-endif
-
-ifeq ($(arch), IA64)
-   CCFLAGS += -DIA64
-endif
-
-ifeq ($(arch), SPARC)
-   CCFLAGS += -DSPARC
-endif
-
+CCFLAGS = -Wall -I../src -finline-functions -O3
 LDFLAGS = -L../src -ludt -lstdc++ -lpthread -lm
 
-ifeq ($(os), UNIX)
-   LDFLAGS += -lsocket
-endif
-
-ifeq ($(os), SUNOS)
-   LDFLAGS += -lrt -lsocket
-endif
-
 DIR = $(shell pwd)
 
 APP = appserver appclient sendfile recvfile test
diff --git a/src/Makefile b/src/Makefile
index fbe57bb..fdc92cf 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -1,34 +1,6 @@
 C++ = g++
 
-ifndef os
-   os = LINUX
-endif
-
-ifndef arch
-   arch = IA32
-endif
-
-CCFLAGS = -fPIC -Wall -Wextra -D$(os) -finline-functions -O3 -fno-strict-aliasing -fvisibility=hidden
-
-ifeq ($(arch), IA32)
-   CCFLAGS += -DIA32
-endif
-
-ifeq ($(arch), POWERPC)
-   CCFLAGS += -mcpu=powerpc
-endif
-
-ifeq ($(arch), SPARC)
-   CCFLAGS += -DSPARC
-endif
-
-ifeq ($(arch), IA64)
-   CCFLAGS += -DIA64
-endif
-
-ifeq ($(arch), AMD64)
-   CCFLAGS += -DAMD64
-endif
+CCFLAGS = -fPIC -Wall -Wextra -finline-functions -O3 -fno-strict-aliasing -fvisibility=hidden
 
 OBJS = api.o buffer.o cache.o ccc.o channel.o common.o core.o epoll.o list.o md5.o packet.o queue.o window.o
 DIR = $(shell pwd)
diff --git a/src/channel.cpp b/src/channel.cpp
index 7b010f0..13e8083 100644
--- a/src/channel.cpp
+++ b/src/channel.cpp
@@ -164,7 +164,7 @@ void CChannel::setUDPSockOpt()
       tv.tv_usec = 100;
    #endif
 
-   #ifdef UNIX
+   #ifdef __unix__
       // Set non-blocking I/O
       // UNIX does not support SO_RCVTIMEO
       int opts = ::fcntl(m_iSocket, F_GETFL);
@@ -292,7 +292,7 @@ int CChannel::recvfrom(sockaddr* addr, CPacket& packet) const
       mh.msg_controllen = 0;
       mh.msg_flags = 0;
 
-      #ifdef UNIX
+      #ifdef __unix__
          fd_set set;
          timeval tv;
          FD_ZERO(&set);
diff --git a/src/common.cpp b/src/common.cpp
index 3b6ffda..315c92d 100644
--- a/src/common.cpp
+++ b/src/common.cpp
@@ -101,7 +101,7 @@ void CTimer::rdtsc(uint64_t &x)
       return;
    }
 
-   #ifdef IA32
+   #if defined(__i386__)
       uint32_t lval, hval;
       //asm volatile ("push %eax; push %ebx; push %ecx; push %edx");
       //asm volatile ("xor %eax, %eax; cpuid");
@@ -109,9 +109,9 @@ void CTimer::rdtsc(uint64_t &x)
       //asm volatile ("pop %edx; pop %ecx; pop %ebx; pop %eax");
       x = hval;
       x = (x << 32) | lval;
-   #elif defined(IA64)
+   #elif defined(__ia64__)
       asm ("mov %0=ar.itc" : "=r"(x) :: "memory");
-   #elif defined(AMD64)
+   #elif defined(__amd64__)
       uint32_t lval, hval;
       asm ("rdtsc" : "=a" (lval), "=d" (hval));
       x = hval;
@@ -135,7 +135,7 @@ uint64_t CTimer::readCPUFrequency()
 {
    uint64_t frequency = 1;  // 1 tick per microsecond.
 
-   #if defined(IA32) || defined(IA64) || defined(AMD64)
+   #if defined(__unix__)
       uint64_t t1, t2;
 
       rdtsc(t1);
@@ -191,11 +191,11 @@ void CTimer::sleepto(uint64_t nexttime)
    while (t < m_ullSchedTime)
    {
       #ifndef NO_BUSY_WAITING
-         #ifdef IA32
+         #if defined(__i386__)
             __asm__ volatile ("pause; rep; nop; nop; nop; nop; nop;");
-         #elif IA64
+         #elif defined(__ia64__)
             __asm__ volatile ("nop 0; nop 0; nop 0; nop 0; nop 0;");
-         #elif AMD64
+         #elif defined(__amd64__)
             __asm__ volatile ("nop; nop; nop; nop; nop;");
          #endif
       #else
diff --git a/src/epoll.cpp b/src/epoll.cpp
index 0e7ddb1..ec1fe14 100644
--- a/src/epoll.cpp
+++ b/src/epoll.cpp
@@ -38,7 +38,7 @@ written by
    Yunhong Gu, last updated 01/01/2011
 *****************************************************************************/
 
-#ifdef LINUX
+#ifdef __linux__
    #include <sys/epoll.h>
    #include <unistd.h>
 #endif
@@ -70,7 +70,7 @@ int CEPoll::create()
 
    int localid = 0;
 
-   #ifdef LINUX
+   #ifdef __linux__
    localid = epoll_create(1024);
    if (localid < 0)
       throw CUDTException(-1, 0, errno);
@@ -115,7 +115,7 @@ int CEPoll::add_ssock(const int eid, const SYSSOCKET& s, const int* events)
    if (p == m_mPolls.end())
       throw CUDTException(5, 13);
 
-#ifdef LINUX
+#ifdef __linux__
    epoll_event ev;
    memset(&ev, 0, sizeof(epoll_event));
 
@@ -165,7 +165,7 @@ int CEPoll::remove_ssock(const int eid, const SYSSOCKET& s)
    if (p == m_mPolls.end())
       throw CUDTException(5, 13);
 
-#ifdef LINUX
+#ifdef __linux__
    epoll_event ev;  // ev is ignored, for compatibility with old Linux kernel only.
    if (::epoll_ctl(p->second.m_iLocalID, EPOLL_CTL_DEL, s, &ev) < 0)
       throw CUDTException();
@@ -227,7 +227,7 @@ int CEPoll::wait(const int eid, set<UDTSOCKET>* readfds, set<UDTSOCKET>* writefd
 
       if (lrfds || lwfds)
       {
-         #ifdef LINUX
+         #ifdef __linux__
          const int max_events = p->second.m_sLocals.size();
          epoll_event ev[max_events];
          int nfds = ::epoll_wait(p->second.m_iLocalID, ev, max_events, 0);
@@ -308,7 +308,7 @@ int CEPoll::release(const int eid)
    if (i == m_mPolls.end())
       throw CUDTException(5, 13);
 
-   #ifdef LINUX
+   #ifdef __linux__
    // release local/system epoll descriptor
    ::close(i->second.m_iLocalID);
    #endif
