[PATCH stable-2.12 0/2] Support different lens-4.* versions

12 views
Skip to first unread message

Petr Pudlak

unread,
Feb 11, 2015, 10:33:53 AM2/11/15
to ganeti...@googlegroups.com
As the interface to TH generation of lenses has changed twice un 4.*, and we
need to support them all, we need to differentiate the code according the actual
lens version.

The first patch is only for 2.12 and 2.13. In 2.14 it should be removed in favor
of macros generated by cabal.

Petr Pudlak (2):
Create a simple CPP macro for detecting lens versions
Construct lenses correctly for different 'lens' versions

Makefile.am | 26 +++++++++++++++++++-------
src/Ganeti/Lens.hs | 25 ++++++++++++++++++++++---
2 files changed, 41 insertions(+), 10 deletions(-)

--
2.2.0.rc0.207.ga3a616c

Petr Pudlak

unread,
Feb 11, 2015, 10:33:56 AM2/11/15
to ganeti...@googlegroups.com
.. in Haskell code.

This patch should be removed starting from 2.14, as there cabal
provides proper macros for all packages.

The macro in this patch is intentionally kept compatible with the cabal
macros.

Signed-off-by: Petr Pudlak <pud...@google.com>
---
Makefile.am | 26 +++++++++++++++++++-------
src/Ganeti/Lens.hs | 10 +++++++++-
2 files changed, 28 insertions(+), 8 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 8033db1..c881e83 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -272,6 +272,7 @@ CLEANFILES = \
$(addsuffix /*.o,$(HS_DIRS)) \
$(addsuffix /*.$(HTEST_SUFFIX)_hi,$(HS_DIRS)) \
$(addsuffix /*.$(HTEST_SUFFIX)_o,$(HS_DIRS)) \
+ hs-lens-version \
Makefile.ghc \
Makefile.ghc.bak \
$(PYTHON_BOOTSTRAP) \
@@ -738,6 +739,11 @@ if HTEST
HFLAGS += -DTEST
endif

+# lens version; this should be removed in 2.14 in favor of cabal-generated
+# versions for all packages
+# see target hs-lens-version
+HFLAGS += $(shell cat hs-lens-version)
+
HTEST_FLAGS = $(HFLAGS) -fhpc -itest/hs \
-osuf .$(HTEST_SUFFIX)_o \
-hisuf .$(HTEST_SUFFIX)_hi
@@ -1171,11 +1177,17 @@ install-exec-hook:

HS_SRCS = $(HS_LIBTESTBUILT_SRCS)

+hs-lens-version:
+ ghc-pkg list --simple-output lens \
+ | sed -r -e 's/^lens-([0-9]+)\.([0-9]+)\.([0-9]+)(\.([0-9]+))?/\
+ -DLENS_MAJOR=\1 -DLENS_MINOR=\2 -DLENS_REV=\3/' \
+ > $@
+
HS_MAKEFILE_GHC_SRCS = $(HS_SRC_PROGS:%=%.hs)
if WANT_HSTESTS
HS_MAKEFILE_GHC_SRCS += $(HS_TEST_PROGS:%=%.hs)
endif
-Makefile.ghc: $(HS_MAKEFILE_GHC_SRCS) Makefile \
+Makefile.ghc: $(HS_MAKEFILE_GHC_SRCS) Makefile hs-lens-version \
| $(built_base_sources) $(HS_BUILT_SRCS)
$(GHC) -M -dep-makefile $@ -dep-suffix $(HPROF_SUFFIX) \
-dep-suffix $(HTEST_SUFFIX) $(HFLAGS) -itest/hs \
@@ -1194,7 +1206,7 @@ Makefile.ghc: $(HS_MAKEFILE_GHC_SRCS) Makefile \

@include_makefile_ghc@

-%.o:
+%.o: hs-lens-version
@echo '[GHC]: $@ <- $^'
@$(GHC) -c $(HFLAGS) \
$(HS_PARALLEL3) $(HS_REGEX_PCRE) $(HEXTRA_COMBINED) $(@:%.o=%.hs)
@@ -1203,7 +1215,7 @@ Makefile.ghc: $(HS_MAKEFILE_GHC_SRCS) Makefile \
# and then once with profiling. See
# http://www.haskell.org/ghc/docs/7.0.4/html/users_guide/template-haskell.html#id636646
if HPROFILE
-%.$(HPROF_SUFFIX)_o: %.o
+%.$(HPROF_SUFFIX)_o: %.o hs-lens-version
@echo '[GHC|prof]: $@ <- $^'
@$(GHC) -c $(HFLAGS) \
$(HPROFFLAGS) \
@@ -1211,7 +1223,7 @@ if HPROFILE
$(@:%.$(HPROF_SUFFIX)_o=%.hs)
endif

-%.$(HTEST_SUFFIX)_o:
+%.$(HTEST_SUFFIX)_o: hs-lens-version
@echo '[GHC|test]: $@ <- $^'
@$(GHC) -c $(HTEST_FLAGS) \
$(HS_PARALLEL3) $(HS_REGEX_PCRE) $(HEXTRA_COMBINED) $(@:%.$(HTEST_SUFFIX)_o=%.hs)
@@ -1223,7 +1235,7 @@ endif
if HPROFILE
$(HS_SRC_PROGS): %: %.$(HPROF_SUFFIX)_o | stamp-directories
else
-$(HS_SRC_PROGS): %: %.o | stamp-directories
+$(HS_SRC_PROGS): %: %.o hs-lens-version | stamp-directories
endif
@echo '[GHC-link]: $@'
$(GHC) $(HFLAGS) \
@@ -1232,7 +1244,7 @@ endif
@rm -f $(notdir $@).tix
@touch "$@"

-$(HS_TEST_PROGS): %: %.$(HTEST_SUFFIX)_o \
+$(HS_TEST_PROGS): %: %.$(HTEST_SUFFIX)_o hs-lens-version \
| stamp-directories $(built_python_sources)
@if [ "$(HS_NODEV)" ]; then \
echo "Error: cannot run unittests without the development" \
@@ -2623,7 +2635,7 @@ $(APIDOC_HS_DIR)/index.html: $(HS_LIBTESTBUILT_SRCS) Makefile
$(HS_LIBTESTBUILT_SRCS)

.PHONY: TAGS
-TAGS: $(GENERATED_FILES)
+TAGS: $(GENERATED_FILES) hs-lens-version
rm -f TAGS
$(GHC) -e ":etags TAGS_hs" -v0 \
$(filter-out -O -Werror,$(HFLAGS)) \
diff --git a/src/Ganeti/Lens.hs b/src/Ganeti/Lens.hs
index b3bd69f..9443309 100644
--- a/src/Ganeti/Lens.hs
+++ b/src/Ganeti/Lens.hs
@@ -1,4 +1,4 @@
-{-# LANGUAGE RankNTypes #-}
+{-# LANGUAGE RankNTypes, CPP #-}

{-| Provides all lens-related functions.

@@ -44,6 +44,14 @@ module Ganeti.Lens
, atSet
) where

+
+-- The following macro is just a temporary solution for 2.12 and 2.13.
+-- Since 2.14 cabal creates proper macros for all dependencies.
+#define MIN_VERSION_lens(maj,min,rev) \
+ (((maj)<LENS_MAJOR)|| \
+ (((maj)==LENS_MAJOR)&&((min)<=LENS_MINOR))|| \
+ (((maj)==LENS_MAJOR)&&((min)==LENS_MINOR)&&((rev)<=LENS_REV)))
+
import Control.Applicative ((<$>), WrappedMonad(..))
import Control.Lens
import Control.Monad
--
2.2.0.rc0.207.ga3a616c

Petr Pudlak

unread,
Feb 11, 2015, 10:33:58 AM2/11/15
to ganeti...@googlegroups.com
As the interface of the TH code for generating lenses changed twice in
4.*, we need to differentiate between versions accordingly.

Signed-off-by: Petr Pudlak <pud...@google.com>
---
src/Ganeti/Lens.hs | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/src/Ganeti/Lens.hs b/src/Ganeti/Lens.hs
index 9443309..0c006d0 100644
--- a/src/Ganeti/Lens.hs
+++ b/src/Ganeti/Lens.hs
@@ -72,8 +72,19 @@ makeCustomLensesFiltered :: (String -> Bool) -> Name -> Q [Dec]
makeCustomLensesFiltered f = makeLensesWith customRules
where
customRules :: LensRules
- customRules = set lensField (fmap lensFieldName . mfilter f . Just)
- defaultRules
+ customRules = set lensField nameFun lensRules
+#if MIN_VERSION_lens(4,5,0)
+ nameFun :: Name -> [Name] -> Name -> [DefName]
+ nameFun _ _ = liftM (TopName . mkName) . nameFilter . nameBase
+#elif MIN_VERSION_lens(4,4,0)
+ nameFun :: [Name] -> Name -> [DefName]
+ nameFun _ = liftM (TopName . mkName) . nameFilter . nameBase
+#else
+ nameFun :: String -> Maybe String
+ nameFun = nameFilter
+#endif
+ nameFilter :: (MonadPlus m) => String -> m String
+ nameFilter = liftM lensFieldName . mfilter f . return

-- | Create lenses for all fields of a given data type.
makeCustomLenses :: Name -> Q [Dec]
--
2.2.0.rc0.207.ga3a616c

Klaus Aehlig

unread,
Feb 11, 2015, 11:20:59 AM2/11/15
to Petr Pudlak, ganeti...@googlegroups.com
On Wed, Feb 11, 2015 at 04:33:40PM +0100, 'Petr Pudlak' via ganeti-devel wrote:
> As the interface of the TH code for generating lenses changed twice in
> 4.*, we need to differentiate between versions accordingly.
>
> Signed-off-by: Petr Pudlak <pud...@google.com>
> ---
> src/Ganeti/Lens.hs | 15 +++++++++++++--
> 1 file changed, 13 insertions(+), 2 deletions(-)

LGTM

--
Klaus Aehlig
Google Germany GmbH, Dienerstr. 12, 80331 Muenchen
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
Geschaeftsfuehrer: Graham Law, Christine Elizabeth Flores

Klaus Aehlig

unread,
Feb 11, 2015, 11:24:24 AM2/11/15
to Petr Pudlak, ganeti...@googlegroups.com
On Wed, Feb 11, 2015 at 04:33:39PM +0100, 'Petr Pudlak' via ganeti-devel wrote:
> .. in Haskell code.
>
> This patch should be removed starting from 2.14, as there cabal
> provides proper macros for all packages.
>
> The macro in this patch is intentionally kept compatible with the cabal
> macros.
>
> Signed-off-by: Petr Pudlak <pud...@google.com>
> ---
> Makefile.am | 26 +++++++++++++++++++-------
> src/Ganeti/Lens.hs | 10 +++++++++-
> 2 files changed, 28 insertions(+), 8 deletions(-)
>
> diff --git a/Makefile.am b/Makefile.am
> index 8033db1..c881e83 100644
> --- a/Makefile.am
> +++ b/Makefile.am
> @@ -272,6 +272,7 @@ CLEANFILES = \
> $(addsuffix /*.o,$(HS_DIRS)) \
> $(addsuffix /*.$(HTEST_SUFFIX)_hi,$(HS_DIRS)) \
> $(addsuffix /*.$(HTEST_SUFFIX)_o,$(HS_DIRS)) \
> + hs-lens-version \
> Makefile.ghc \
> Makefile.ghc.bak \
> $(PYTHON_BOOTSTRAP) \

Normally we also .gitignore automatically generated files.
This is bad Makefile style: you add the explicit dependency here (because
you have to) but you hide the actual usage in a unrelated variable; this
is hard for maintainance having to guess that hs-lens-version is used in
HFLAGS. Can we just make the use explicit and drop adding the call to HFLAGS?

> @echo '[GHC]: $@ <- $^'
> @$(GHC) -c $(HFLAGS) \
> $(HS_PARALLEL3) $(HS_REGEX_PCRE) $(HEXTRA_COMBINED) $(@:%.o=%.hs)

Thanks,
Klaus

Petr Pudlak

unread,
Feb 11, 2015, 11:42:27 AM2/11/15
to Klaus Aehlig, ganeti...@googlegroups.com
ACK. Since the interdiff would be probably more messy than the original
patch, I'm resending the whole patch below:


diff --git a/.gitignore b/.gitignore
index 5e7756a..0b65795 100644
--- a/.gitignore
+++ b/.gitignore
@@ -23,6 +23,7 @@
# /
/.hsenv
/Makefile
+/hs-lens-version
/Makefile.ghc
/Makefile.ghc.bak
/Makefile.in
diff --git a/Makefile.am b/Makefile.am
index 8033db1..5980819 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -272,6 +272,7 @@ CLEANFILES = \
$(addsuffix /*.o,$(HS_DIRS)) \
$(addsuffix /*.$(HTEST_SUFFIX)_hi,$(HS_DIRS)) \
$(addsuffix /*.$(HTEST_SUFFIX)_o,$(HS_DIRS)) \
+ hs-lens-version \
Makefile.ghc \
Makefile.ghc.bak \
$(PYTHON_BOOTSTRAP) \
@@ -1171,14 +1172,21 @@ install-exec-hook:

HS_SRCS = $(HS_LIBTESTBUILT_SRCS)

+hs-lens-version:
+ ghc-pkg list --simple-output lens \
+ | sed -r -e 's/^lens-([0-9]+)\.([0-9]+)\.([0-9]+)(\.([0-9]+))?/\
+ -DLENS_MAJOR=\1 -DLENS_MINOR=\2 -DLENS_REV=\3/' \
+ > $@
+
HS_MAKEFILE_GHC_SRCS = $(HS_SRC_PROGS:%=%.hs)
if WANT_HSTESTS
HS_MAKEFILE_GHC_SRCS += $(HS_TEST_PROGS:%=%.hs)
endif
-Makefile.ghc: $(HS_MAKEFILE_GHC_SRCS) Makefile \
+Makefile.ghc: $(HS_MAKEFILE_GHC_SRCS) Makefile hs-lens-version \
| $(built_base_sources) $(HS_BUILT_SRCS)
$(GHC) -M -dep-makefile $@ -dep-suffix $(HPROF_SUFFIX) \
-dep-suffix $(HTEST_SUFFIX) $(HFLAGS) -itest/hs \
+ $(shell cat hs-lens-version) \
$(HS_PARALLEL3) $(HS_REGEX_PCRE) $(HEXTRA_COMBINED) $(HS_MAKEFILE_GHC_SRCS)
# Since ghc -M does not generate dependency line for object files, dependencies
# from a target executable seed object (e.g. src/hluxid.o) to objects which
@@ -1194,26 +1202,29 @@ Makefile.ghc: $(HS_MAKEFILE_GHC_SRCS) Makefile \

@include_makefile_ghc@

-%.o:
+%.o: hs-lens-version
@echo '[GHC]: $@ <- $^'
@$(GHC) -c $(HFLAGS) \
+ $(shell cat hs-lens-version) \
$(HS_PARALLEL3) $(HS_REGEX_PCRE) $(HEXTRA_COMBINED) $(@:%.o=%.hs)

# For TH+profiling we need to compile twice: Once without profiling,
# and then once with profiling. See
# http://www.haskell.org/ghc/docs/7.0.4/html/users_guide/template-haskell.html#id636646
if HPROFILE
-%.$(HPROF_SUFFIX)_o: %.o
+%.$(HPROF_SUFFIX)_o: %.o hs-lens-version
@echo '[GHC|prof]: $@ <- $^'
@$(GHC) -c $(HFLAGS) \
+ $(shell cat hs-lens-version) \
$(HPROFFLAGS) \
$(HS_PARALLEL3) $(HS_REGEX_PCRE) $(HEXTRA_COMBINED) \
$(@:%.$(HPROF_SUFFIX)_o=%.hs)
endif

-%.$(HTEST_SUFFIX)_o:
+%.$(HTEST_SUFFIX)_o: hs-lens-version
@echo '[GHC|test]: $@ <- $^'
@$(GHC) -c $(HTEST_FLAGS) \
+ $(shell cat hs-lens-version) \
$(HS_PARALLEL3) $(HS_REGEX_PCRE) $(HEXTRA_COMBINED) $(@:%.$(HTEST_SUFFIX)_o=%.hs)

%.hi: %.o ;
@@ -1223,16 +1234,17 @@ endif
if HPROFILE
$(HS_SRC_PROGS): %: %.$(HPROF_SUFFIX)_o | stamp-directories
else
-$(HS_SRC_PROGS): %: %.o | stamp-directories
+$(HS_SRC_PROGS): %: %.o hs-lens-version | stamp-directories
endif
@echo '[GHC-link]: $@'
$(GHC) $(HFLAGS) \
+ $(shell cat hs-lens-version) \
$(HPROFFLAGS) \
$(HS_PARALLEL3) $(HS_REGEX_PCRE) $(HEXTRA_COMBINED) --make $(@:%=%.hs)
@rm -f $(notdir $@).tix
@touch "$@"

-$(HS_TEST_PROGS): %: %.$(HTEST_SUFFIX)_o \
+$(HS_TEST_PROGS): %: %.$(HTEST_SUFFIX)_o hs-lens-version \
| stamp-directories $(built_python_sources)
@if [ "$(HS_NODEV)" ]; then \
echo "Error: cannot run unittests without the development" \
@@ -1241,6 +1253,7 @@ $(HS_TEST_PROGS): %: %.$(HTEST_SUFFIX)_o \
fi
@echo '[GHC-link|test]: $@'
$(GHC) $(HTEST_FLAGS) \
+ $(shell cat hs-lens-version) \
$(HS_PARALLEL3) $(HS_REGEX_PCRE) $(HEXTRA_COMBINED) --make $(@:%=%.hs)
@rm -f $(notdir $@).tix
@touch "$@"
@@ -2623,10 +2636,11 @@ $(APIDOC_HS_DIR)/index.html: $(HS_LIBTESTBUILT_SRCS) Makefile
$(HS_LIBTESTBUILT_SRCS)

.PHONY: TAGS
-TAGS: $(GENERATED_FILES)
+TAGS: $(GENERATED_FILES) hs-lens-version
rm -f TAGS
$(GHC) -e ":etags TAGS_hs" -v0 \
$(filter-out -O -Werror,$(HFLAGS)) \
+ $(shell cat hs-lens-version) \
-osuf tags.o \
-hisuf tags.hi \
-lcurl \

Klaus T. Aehlig

unread,
Feb 12, 2015, 3:40:02 AM2/12/15
to ganeti...@googlegroups.com, pud...@google.com

> ACK. Since the interdiff would be probably more messy than the original
> patch, I'm resending the whole patch below: [...]

Thanks for changing the patch. LGTM.
Reply all
Reply to author
Forward
0 new messages