Skip to content

Commit ca632d0

Browse files
committed
Patches for compiling to wasm
1 parent 74ba1f3 commit ca632d0

File tree

10 files changed

+81
-29
lines changed

10 files changed

+81
-29
lines changed

cardano-diffusion/lib/Cardano/Network/Diffusion/Handlers.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE CPP #-}
2+
{-# OPTIONS_GHC -Wno-deprecations #-}
23

34
#if !defined(mingw32_HOST_OS)
45
#define POSIX

cardano-diffusion/lib/Cardano/Network/NodeToClient.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE CPP #-}
12
{-# LANGUAGE DataKinds #-}
23
{-# LANGUAGE GADTs #-}
34
{-# LANGUAGE NamedFieldPuns #-}
@@ -171,7 +172,11 @@ nodeToClientProtocols protocols _version _versionData =
171172
maximumMiniProtocolLimits :: MiniProtocolLimits
172173
maximumMiniProtocolLimits =
173174
MiniProtocolLimits {
175+
#if !defined(wasm32_HOST_ARCH)
174176
maximumIngressQueue = 0xffffffff
177+
#else
178+
maximumIngressQueue = 0x7fffffff
179+
#endif
175180
}
176181

177182

cardano-diffusion/lib/Cardano/Network/NodeToNode.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE CPP #-}
12
{-# LANGUAGE DataKinds #-}
23
{-# LANGUAGE FlexibleInstances #-}
34
{-# LANGUAGE NamedFieldPuns #-}
@@ -77,7 +78,11 @@ import Data.ByteString.Lazy qualified as BL
7778
import Data.Word
7879

7980
import Network.Mux qualified as Mx
81+
#if !defined(wasm32_HOST_ARCH)
8082
import Network.Socket (Socket, StructLinger (..))
83+
#else
84+
import Network.Socket (Socket)
85+
#endif
8186
import Network.Socket qualified as Socket
8287

8388
import Cardano.Network.NodeToNode.Version
@@ -401,6 +406,7 @@ connectTo
401406
-> Maybe Socket.SockAddr
402407
-> Socket.SockAddr
403408
-> IO (Either SomeException (Either a b))
409+
#if !defined(wasm32_HOST_ARCH)
404410
connectTo sn tr =
405411
connectToNode sn makeSocketBearer
406412
ConnectToArgs {
@@ -418,7 +424,9 @@ connectTo sn tr =
418424
Socket.setSockOpt sock Socket.Linger
419425
(StructLinger { sl_onoff = 1,
420426
sl_linger = 0 })
421-
427+
#else
428+
connectTo _ _ = error "connecTo not supported in wasm"
429+
#endif
422430
-- | Node-To-Node protocol connections which negotiated
423431
-- `InitiatorAndResponderDiffusionMode` are `Duplex`.
424432
--

ouroboros-network/demo/connection-manager.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
{-# LANGUAGE TupleSections #-}
1313
{-# LANGUAGE TypeApplications #-}
1414
{-# LANGUAGE TypeOperators #-}
15+
{-# OPTIONS_GHC -Wno-deprecations #-}
1516

1617
-- just to use 'debugTracer'
1718
{-# OPTIONS_GHC -Wno-redundant-constraints #-}

ouroboros-network/framework/io-tests/Test/Ouroboros/Network/Server/IO.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
{-# LANGUAGE PolyKinds #-}
77
{-# LANGUAGE RankNTypes #-}
88
{-# LANGUAGE ScopedTypeVariables #-}
9+
{-# OPTIONS_GHC -Wno-deprecations #-}
910

1011
#if __GLASGOW_HASKELL__ >= 908
1112
{-# OPTIONS_GHC -Wno-x-partial #-}

ouroboros-network/framework/io-tests/Test/Ouroboros/Network/Socket.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
{-# LANGUAGE GADTs #-}
77
{-# LANGUAGE NamedFieldPuns #-}
88
{-# LANGUAGE ScopedTypeVariables #-}
9+
{-# OPTIONS_GHC -Wno-deprecations #-}
910

1011
{-# OPTIONS_GHC -Wno-orphans #-}
1112
module Test.Ouroboros.Network.Socket (tests) where

ouroboros-network/framework/lib/Ouroboros/Network/Snocket.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
{-# LANGUAGE ScopedTypeVariables #-}
1111
{-# LANGUAGE StandaloneDeriving #-}
1212
{-# LANGUAGE TypeSynonymInstances #-}
13+
{-# OPTIONS_GHC -Wno-deprecations #-}
1314

1415
#if !defined(mingw32_HOST_OS)
1516
#define POSIX

ouroboros-network/framework/lib/Ouroboros/Network/Socket.hs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ import Codec.CBOR.Read qualified as CBOR
6161
import Codec.CBOR.Term qualified as CBOR
6262
import Control.Applicative (Alternative)
6363
import Control.Concurrent.Class.MonadSTM.Strict
64+
#if !defined(wasm32_HOST_ARCH)
6465
import Control.Monad (unless, when)
66+
#endif
6567
import Control.Monad.Class.MonadAsync
6668
import Control.Monad.Class.MonadThrow
6769
import Control.Monad.Class.MonadTime.SI
@@ -72,8 +74,11 @@ import Data.Hashable
7274
import Data.Monoid.Synchronisation (FirstToFinish (..))
7375
import Data.Typeable (Typeable)
7476
import Data.Word (Word16)
75-
77+
#if !defined(wasm32_HOST_ARCH)
7678
import Network.Socket (SockAddr, Socket, StructLinger (..))
79+
#else
80+
import Network.Socket (SockAddr, Socket)
81+
#endif
7782
import Network.Socket qualified as Socket
7883

7984
import Control.Tracer
@@ -136,6 +141,7 @@ sockAddrFamily Socket.SockAddrUnix {} = Socket.AF_UNIX
136141
-- is expected.
137142
--
138143
configureSocket :: Socket -> Maybe SockAddr -> IO ()
144+
#if !defined(wasm32_HOST_ARCH)
139145
configureSocket sock addr = do
140146
let fml = sockAddrFamily <$> addr
141147
Socket.setSocketOption sock Socket.ReuseAddr 1
@@ -157,14 +163,18 @@ configureSocket sock addr = do
157163
-- it is enabled by default on some systems. Disabled here since we run a separate
158164
-- IPv4 server instance if configured to use IPv4.
159165
$ Socket.setSocketOption sock Socket.IPv6Only 1
160-
166+
#else
167+
configureSocket _ _ =
168+
error "configureSocket not supported in wasm"
169+
#endif
161170

162171
-- | Configure sockets passed through systemd socket activation.
163172
-- Currently 'ReuseAddr' and 'Linger' options are not configurable with
164173
-- 'systemd.socket', these options are set by this function. For other socket
165174
-- options we only trace if they are not set.
166175
--
167176
configureSystemdSocket :: Tracer IO SystemdSocketTracer -> Socket -> SockAddr -> IO ()
177+
#if !defined(wasm32_HOST_ARCH)
168178
configureSystemdSocket tracer sock addr = do
169179
let fml = sockAddrFamily addr
170180
case fml of
@@ -190,6 +200,10 @@ configureSystemdSocket tracer sock addr = do
190200
ipv6OnlyOpt <- Socket.getSocketOption sock Socket.IPv6Only
191201
unless (ipv6OnlyOpt /= 0) $
192202
traceWith tracer (SocketOptionNotSet Socket.IPv6Only)
203+
#else
204+
configureSystemdSocket _ _ _ =
205+
error "configureSystemdSocket not supported in wasm"
206+
#endif
193207

194208
data SystemdSocketTracer = SocketOptionNotSet Socket.SocketOption
195209
deriving Show

ouroboros-network/ouroboros-network.cabal

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -464,9 +464,10 @@ test-suite tests-lib-tests
464464
tasty,
465465
tasty-quickcheck,
466466

467-
ghc-options:
468-
-rtsopts
469-
-threaded
467+
if !arch(wasm32)
468+
ghc-options:
469+
-rtsopts
470+
-threaded
470471

471472
library framework-tests-lib
472473
import: ghc-options
@@ -532,9 +533,10 @@ test-suite framework-sim-tests
532533
typed-protocols:{typed-protocols, cborg, examples},
533534
with-utf8,
534535

535-
ghc-options:
536-
-rtsopts
537-
-threaded
536+
if !arch(wasm32)
537+
ghc-options:
538+
-rtsopts
539+
-threaded
538540

539541
if flag(ipv6)
540542
cpp-options: -DOUROBOROS_NETWORK_IPV6
@@ -574,6 +576,8 @@ test-suite framework-io-tests
574576
ghc-options:
575577
-rtsopts
576578
-threaded
579+
if arch(wasm32)
580+
buildable: False
577581

578582
library orphan-instances
579583
import: ghc-options
@@ -632,6 +636,8 @@ executable demo-ping-pong
632636
network-mux,
633637
ouroboros-network:{api, framework},
634638
typed-protocols:examples,
639+
if arch(wasm32)
640+
buildable: False
635641

636642
executable demo-connection-manager
637643
import: ghc-options
@@ -811,10 +817,13 @@ test-suite protocols-tests
811817
tasty,
812818

813819
ghc-options:
814-
-threaded
815820
-Wall
816821
-Wunused-packages
817-
-rtsopts
822+
823+
if !arch(wasm32)
824+
ghc-options:
825+
-threaded
826+
-rtsopts
818827

819828
-- Simulation Test Library
820829
library ouroboros-network-tests-lib
@@ -895,11 +904,13 @@ test-suite ouroboros-network-sim-tests
895904

896905
ghc-options:
897906
-fno-ignore-asserts
898-
-threaded
899-
-rtsopts
900-
+RTS
901-
-T
902-
-RTS
907+
if !arch(wasm32)
908+
ghc-options:
909+
-threaded
910+
-rtsopts
911+
+RTS
912+
-T
913+
-RTS
903914

904915
-- Tests which require system calls provided by `Win32-network` or `network`
905916
-- library. These tests are compiled natively & run on all supported
@@ -939,12 +950,13 @@ test-suite ouroboros-network-io-tests
939950
else
940951
build-depends: process
941952

942-
ghc-options:
943-
-threaded
944-
-rtsopts
945-
+RTS
946-
-T
947-
-RTS
953+
if !arch(wasm32)
954+
ghc-options:
955+
-threaded
956+
-rtsopts
957+
+RTS
958+
-T
959+
-RTS
948960

949961
benchmark sim-benchmarks
950962
import: ghc-options-tests
@@ -964,10 +976,12 @@ benchmark sim-benchmarks
964976
-- https://gitlab.haskell.org/ghc/ghc/-/issues/25165
965977
ghc-options:
966978
-fno-ignore-asserts
967-
-threaded
968-
-rtsopts
969-
-with-rtsopts=-A32m
970-
-fproc-alignment=64
971-
+RTS
972-
-T
973-
-RTS
979+
if !arch(wasm32)
980+
ghc-options:
981+
-threaded
982+
-rtsopts
983+
-with-rtsopts=-A32m
984+
-fproc-alignment=64
985+
+RTS
986+
-T
987+
-RTS

ouroboros-network/tests/io/Test/Ouroboros/Network/Socket.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE CPP #-}
12
{-# LANGUAGE BangPatterns #-}
23
{-# LANGUAGE DataKinds #-}
34
{-# LANGUAGE FlexibleContexts #-}
@@ -7,6 +8,7 @@
78
{-# LANGUAGE TupleSections #-}
89
{-# LANGUAGE TypeFamilies #-}
910

11+
{-# OPTIONS_GHC -Wno-deprecations #-}
1012
{-# OPTIONS_GHC -Wno-orphans #-}
1113
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
1214
module Test.Ouroboros.Network.Socket (tests) where
@@ -129,7 +131,11 @@ testVersionCodecCBORTerm !_ =
129131
decodeTerm :: CBOR.Term -> Either Text TestVersionData
130132
decodeTerm (CBOR.TList [CBOR.TInt x])
131133
| x >= 0
134+
#if !defined(wasm32_HOST_ARCH)
132135
, x <= 0xffffffff
136+
#else
137+
, x <= 0x7fffffff
138+
#endif
133139
= Right
134140
TestVersionData {
135141
networkMagic = NetworkMagic (fromIntegral x)

0 commit comments

Comments
 (0)