summaryrefslogtreecommitdiff
path: root/testsuite/tests/plugins
diff options
context:
space:
mode:
authorSylvain Henry <sylvain@haskus.fr>2022-01-14 14:19:11 +0100
committerMarge Bot <ben+marge-bot@smart-cactus.org>2022-08-10 09:44:46 -0400
commitf95bbdcae3e6710a92dd8244321677eef91890de (patch)
tree214a46a6f0549baf14d2749bdd60124eb03c591e /testsuite/tests/plugins
parent823fe5b56450a7eefbf41ce8ece34095bf2217ee (diff)
downloadhaskell-f95bbdcae3e6710a92dd8244321677eef91890de.tar.gz
Add support for external static plugins (#20964)
This patch adds a new command-line flag: -fplugin-library=<file-path>;<unit-id>;<module>;<args> used like this: -fplugin-library=path/to/plugin.so;package-123;Plugin.Module;["Argument","List"] It allows a plugin to be loaded directly from a shared library. With this approach, GHC doesn't compile anything for the plugin and doesn't load any .hi file for the plugin and its dependencies. As such GHC doesn't need to support two environments (one for plugins, one for target code), which was the more ambitious approach tracked in #14335. Fix #20964 Co-authored-by: Josh Meredith <joshmeredith2008@gmail.com>
Diffstat (limited to 'testsuite/tests/plugins')
-rw-r--r--testsuite/tests/plugins/Makefile15
-rw-r--r--testsuite/tests/plugins/all.T5
-rw-r--r--testsuite/tests/plugins/plugins-external.hs4
-rw-r--r--testsuite/tests/plugins/plugins-external.stderr2
-rw-r--r--testsuite/tests/plugins/plugins-external.stdout1
-rw-r--r--testsuite/tests/plugins/shared-plugin/LICENSE10
-rw-r--r--testsuite/tests/plugins/shared-plugin/Makefile20
-rw-r--r--testsuite/tests/plugins/shared-plugin/Setup.hs3
-rw-r--r--testsuite/tests/plugins/shared-plugin/Simple/Plugin.hs26
-rw-r--r--testsuite/tests/plugins/shared-plugin/shared-plugin.cabal21
10 files changed, 107 insertions, 0 deletions
diff --git a/testsuite/tests/plugins/Makefile b/testsuite/tests/plugins/Makefile
index 482d543c6a..986acba472 100644
--- a/testsuite/tests/plugins/Makefile
+++ b/testsuite/tests/plugins/Makefile
@@ -205,3 +205,18 @@ test-echo-in-turn-many-args:
.PHONY: test-echo-in-line-many-args
test-echo-in-line-many-args:
"$(TEST_HC)" $(TEST_HC_OPTS) $(ghcPluginWayFlags) -v0 test-echo-in-line-many-args.hs -package-db echo-plugin/pkg.test-echo-in-line-many-args/local.package.conf
+
+
+ifeq "$(WINDOWS)" "YES"
+DLL = $1.dll
+else ifeq "$(DARWIN)" "YES"
+DLL = lib$1.dylib
+else
+DLL = lib$1.so
+endif
+
+.PHONY: plugins-external
+plugins-external:
+ cp shared-plugin/pkg.plugins01/dist/build/$(call DLL,HSsimple-plugin*) $(call DLL,HSsimple-plugin)
+ "$(TEST_HC)" $(TEST_HC_OPTS) $(ghcPluginWayFlags) --make -v0 -fplugin-library "$(PWD)/$(call DLL,HSsimple-plugin);simple-plugin-1234;Simple.Plugin;[\"Plugin\",\"loaded\",\"from\",\"a shared lib\"]" plugins-external.hs
+ ./plugins-external
diff --git a/testsuite/tests/plugins/all.T b/testsuite/tests/plugins/all.T
index dca05d885c..c503397e78 100644
--- a/testsuite/tests/plugins/all.T
+++ b/testsuite/tests/plugins/all.T
@@ -311,3 +311,8 @@ test('test-echo-in-line-many-args',
[extra_files(['echo-plugin/']),
pre_cmd('$MAKE -s --no-print-directory -C echo-plugin package.test-echo-in-line-many-args TOP={top}')],
makefile_test, [])
+
+test('plugins-external',
+ [extra_files(['shared-plugin/']),
+ pre_cmd('$MAKE -s --no-print-directory -C shared-plugin package.plugins01 TOP={top}')],
+ makefile_test, [])
diff --git a/testsuite/tests/plugins/plugins-external.hs b/testsuite/tests/plugins/plugins-external.hs
new file mode 100644
index 0000000000..9626667e59
--- /dev/null
+++ b/testsuite/tests/plugins/plugins-external.hs
@@ -0,0 +1,4 @@
+-- Intended to test that we can load plugins as external shared libraries
+module Main where
+
+main = putStrLn "Hello World"
diff --git a/testsuite/tests/plugins/plugins-external.stderr b/testsuite/tests/plugins/plugins-external.stderr
new file mode 100644
index 0000000000..ed60f9afab
--- /dev/null
+++ b/testsuite/tests/plugins/plugins-external.stderr
@@ -0,0 +1,2 @@
+Simple Plugin Passes Queried
+Got options: Plugin loaded from a shared lib
diff --git a/testsuite/tests/plugins/plugins-external.stdout b/testsuite/tests/plugins/plugins-external.stdout
new file mode 100644
index 0000000000..557db03de9
--- /dev/null
+++ b/testsuite/tests/plugins/plugins-external.stdout
@@ -0,0 +1 @@
+Hello World
diff --git a/testsuite/tests/plugins/shared-plugin/LICENSE b/testsuite/tests/plugins/shared-plugin/LICENSE
new file mode 100644
index 0000000000..6297f71b3f
--- /dev/null
+++ b/testsuite/tests/plugins/shared-plugin/LICENSE
@@ -0,0 +1,10 @@
+Copyright (c) 2008, Max Bolingbroke
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+ * Neither the name of Max Bolingbroke nor the names of other contributors may be used to endorse or promote products derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/testsuite/tests/plugins/shared-plugin/Makefile b/testsuite/tests/plugins/shared-plugin/Makefile
new file mode 100644
index 0000000000..ae5c24e87f
--- /dev/null
+++ b/testsuite/tests/plugins/shared-plugin/Makefile
@@ -0,0 +1,20 @@
+TOP=../../..
+include $(TOP)/mk/boilerplate.mk
+include $(TOP)/mk/test.mk
+
+clean.%:
+ rm -rf pkg.$*
+
+HERE := $(abspath .)
+$(eval $(call canonicalise,HERE))
+
+package.%:
+ $(MAKE) -s --no-print-directory clean.$*
+ mkdir pkg.$*
+ "$(TEST_HC)" -outputdir pkg.$* --make -v0 -o pkg.$*/setup Setup.hs
+
+ "$(GHC_PKG)" init pkg.$*/local.package.conf
+
+ pkg.$*/setup configure --distdir pkg.$*/dist -v0 $(CABAL_PLUGIN_BUILD) --prefix="$(HERE)/pkg.$*/install" --with-compiler="$(TEST_HC)" --with-hc-pkg="$(GHC_PKG)" --package-db=pkg.$*/local.package.conf $(if $(findstring YES,$(HAVE_PROFILING)), --enable-library-profiling)
+ pkg.$*/setup build --distdir pkg.$*/dist -v0
+ pkg.$*/setup install --distdir pkg.$*/dist -v0
diff --git a/testsuite/tests/plugins/shared-plugin/Setup.hs b/testsuite/tests/plugins/shared-plugin/Setup.hs
new file mode 100644
index 0000000000..e8ef27dbba
--- /dev/null
+++ b/testsuite/tests/plugins/shared-plugin/Setup.hs
@@ -0,0 +1,3 @@
+import Distribution.Simple
+
+main = defaultMain
diff --git a/testsuite/tests/plugins/shared-plugin/Simple/Plugin.hs b/testsuite/tests/plugins/shared-plugin/Simple/Plugin.hs
new file mode 100644
index 0000000000..09ff028e33
--- /dev/null
+++ b/testsuite/tests/plugins/shared-plugin/Simple/Plugin.hs
@@ -0,0 +1,26 @@
+{-# LANGUAGE TemplateHaskell #-}
+
+module Simple.Plugin(plugin) where
+
+import GHC.Types.Unique.FM
+import GHC.Plugins
+import qualified GHC.Utils.Error
+
+import Control.Monad
+import Data.Monoid hiding (Alt)
+import Data.Dynamic
+import qualified Language.Haskell.TH as TH
+
+plugin :: Plugin
+plugin = defaultPlugin {
+ installCoreToDos = install,
+ pluginRecompile = purePlugin
+ }
+
+install :: [CommandLineOption] -> [CoreToDo] -> CoreM [CoreToDo]
+install options todos = do
+ putMsgS $ "Simple Plugin Passes Queried"
+ putMsgS $ "Got options: " ++ unwords options
+
+ -- Create some actual passes to continue the test.
+ return todos
diff --git a/testsuite/tests/plugins/shared-plugin/shared-plugin.cabal b/testsuite/tests/plugins/shared-plugin/shared-plugin.cabal
new file mode 100644
index 0000000000..85d8532973
--- /dev/null
+++ b/testsuite/tests/plugins/shared-plugin/shared-plugin.cabal
@@ -0,0 +1,21 @@
+Name: simple-plugin
+Version: 0.1
+Synopsis: A demonstration of the GHC plugin system.
+Cabal-Version: >= 1.2
+Build-Type: Simple
+License: BSD3
+License-File: LICENSE
+Author: Max Bolingbroke
+Homepage: http://blog.omega-prime.co.uk
+
+Library
+ Extensions: CPP
+ Build-Depends:
+ base,
+ template-haskell,
+ ghc >= 6.11
+ Exposed-Modules:
+ Simple.Plugin
+
+ -- explicitly set the unit-id to allow loading from a shared library
+ ghc-options: -this-unit-id simple-plugin-1234