Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re^3: Defining an XS symbol in the Makefile.PL

by syphilis (Bishop)
on Aug 18, 2019 at 07:15 UTC ( #11104639=note: print w/replies, xml ) Need Help??

in reply to Re^2: Defining an XS symbol in the Makefile.PL
in thread Defining an XS symbol in the Makefile.PL

Generating a header file to be included by the C/XS code seems to be the more portable approach

That's probably sort of what I'm doing - and my initial post oversimplified things.
See this Scalar-List-Utils bug report for a better picture.

The real issue is that MY_FORMAT needs to be defined to "%.17" (if NV is 64 bit), to be defined to "%.21" (if NV is 80 bit), to be defined to "%.36" (if NV is 128 bit).
It's quite easy during the Makefile.PL processing to determine which it needs to be - and surely the simplest thing to do is to just have the Makefile.PL define MY_FORMAT to the correct string.
But you're right ... and jcb's suggestion (along with several variations thereof) is not working.

At the moment, the Makefile.PL is doing:
.... if($Config{nvsize} == 8) { $nvtype = 'LU_NV_IS_64_BIT' } # double o +r 8-byte long double elsif(length(sqrt 2) > 25) { $nvtype = 'LU_NV_IS_128_BIT' } # IEEE lon +g double or __float128 else { $nvtype = 'LU_NV_IS_80_BIT' } # extended + precision long double $defines .= " -D$nvtype"; .... WriteMakefile ( .... DEFINE => $defines, .... );
And the XS file is rewritten to redefine the symbol to the appropriate value:
#if defined(LU_NV_IS_64_BIT) #define MY_FORMAT "%.17" #elif defined(LU_NV_IS_80_BIT) #define MY_FORMAT "%.21" #else #define MY_FORMAT "%.36" #endif
It's working fine .... but I feel that I should be able to define MY_FORMAT directly within the Makefile.PL and avoid having to add that preprocessing to the XS file ??


Replies are listed 'Best First'.
Re^4: Defining an XS symbol in the Makefile.PL
by jcb (Chaplain) on Aug 18, 2019 at 07:29 UTC

    Actually, what you are actually doing is probably a better option. Getting strings through varying shell quoting rules is asking for trouble.

    And that leads to another option:

    in Makefile.PL:

    if($Config{nvsize} == 8) { $nvbits = 64 } elsif(length(sqrt 2) > 25) { $nvbits = 128 } else { $nvbits = 80 } $defines .= ' -DLU_NV_BITS='.$nvbits;

    in XS code:

    #if LU_NV_BITS == 64 #define MY_FORMAT "%.17" #elif LU_NV_BITS == 80 #define MY_FORMAT "%.21" #elif LU_NV_BITS == 128 #define MY_FORMAT "%.36" #else #error "LU_NV_BITS not set" #endif

    Passing a numeric parameter through the shell does not require quoting at all.

    You could also just pass the number of digits you want, but converting that back to a string in the C preprocessor requires macro tricks documented in the GNU CPP manual.

      Actually, what you are actually doing is probably a better option

      Yes, it now looks that way to me. I had no idea it would be so difficult, yet I still haven't found a way.
      q["-DMY_FORMAT=""%.16e"""] doesn't work either.

      I think it's time to give up on my initial idea and just stick with what I've got.
      Thanks for the feedback. I'll go with that variation you posted as it reads a bit better - though I don't want the #error option.
      (If LU_NV_BITS is neither 64 nor 80 then we simply set MY_FORMAT to "%.36" safe in the knowledge that it will be sufficient.)

      Update: I changed my mind and included the "#error" part of the pre-processing. It makes good defensive sense to include it.


        The main reason that I included an #error directive is to catch the case where LU_NV_BITS has not been set at all.

        Also, I simply copied your probe logic, but I must ask if you are sure that that is the right way to do it. Reading a value from %Config for one test, evaluating an expression for another, and then just using a default seems strange to me. Are you sure $Config{nvsize} will not give you 10 and 16 for the 80 and 128 bit cases?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11104639]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2019-10-17 22:59 GMT
Find Nodes?
    Voting Booth?